无法通过didSelectRow tableView方法显示视图控制器

问题描述 投票:1回答:1

我正在构建一个meme生成器应用程序。我有一个meme生成器屏幕,由任一标签栏视图控制器屏幕内的导航栏按钮项调用。第一个标签栏屏幕在表格视图中显示保存的模因,第二个标签栏在集合视图中显示保存的模因。我正在尝试添加一个详细信息视图控制器,当用户点击表视图控制器上的行时显示已保存的模因,当前,我的应用程序正在崩溃触摸事件。

这是我的表视图控制器中的代码:

import UIKit

class TableViewMemesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var memes: [Meme]! {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        memes = appDelegate.memes
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return memes.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        let meme = memes[indexPath.row]
        cell?.imageView?.image = meme.memedImage
        cell?.textLabel?.text = meme.topText
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let meme = memes[indexPath.row]
        let detailController = storyboard!.instantiateViewController(withIdentifier: "DetailsCollectionViewCell") as! DetailsCollectionViewCell
        detailController.detailsImageView.image = meme.memedImage
        present(detailController, animated: true, completion: nil)
    }

}

这是我的详细信息视图控制器中的代码:

import UIKit

class DetailsCollectionViewCell: UIViewController {

    @IBOutlet weak var detailsImageView: UIImageView!


}

当我点击相应的行时,应用程序崩溃,我收到以下错误:

2018-01-01 14:58:28.569113-0600 memeGenerator[6120:358774] [MC] Lazy loading NSBundle MobileCoreServices.framework
2018-01-01 14:58:28.570701-0600 memeGenerator[6120:358774] [MC] Loaded MobileCoreServices.framework
2018-01-01 14:58:32.459620-0600 memeGenerator[6120:358774] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/angelatuzson/Library/Developer/CoreSimulator/Devices/DEDF59A7-71B0-4C2B-85AF-8A877A7426C2/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-01-01 14:58:32.460974-0600 memeGenerator[6120:358774] [MC] Reading from private effective user settings.
2018-01-01 14:58:35.347192-0600 memeGenerator[6120:358824] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
1
2018-01-01 14:58:40.220065-0600 memeGenerator[6120:358774] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x600000469600>) doesn't contain a view controller with identifier 'DetailsCollectionViewCell''
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d91f12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x0000000109518f41 objc_exception_throw + 48
    2   UIKit                               0x000000010ade90d7 -[UIStoryboard instantiateInitialViewController] + 0
    3   memeGenerator                       0x0000000108bd89fe _T013memeGenerator014TableViewMemesD10ControllerC05tableD0ySo07UITableD0C_10Foundation9IndexPathV14didSelectRowAttF + 718
    4   memeGenerator                       0x0000000108bd903c _T013memeGenerator014TableViewMemesD10ControllerC05tableD0ySo07UITableD0C_10Foundation9IndexPathV14didSelectRowAttFTo + 92
    5   UIKit                               0x000000010a60a839 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1810
    6   UIKit                               0x000000010a60aa54 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 344
    7   UIKit                               0x000000010a4d3d59 _runAfterCACommitDeferredBlocks + 318
    8   UIKit                               0x000000010a4c2bb1 _cleanUpAfterCAFlushAndRunDeferredBlocks + 280
    9   UIKit                               0x000000010a4f20e0 _afterCACommitHandler + 137
    10  CoreFoundation                      0x000000010d8c1c07 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    11  CoreFoundation                      0x000000010d8c1b5e __CFRunLoopDoObservers + 430
    12  CoreFoundation                      0x000000010d8a6124 __CFRunLoopRun + 1572
    13  CoreFoundation                      0x000000010d8a5889 CFRunLoopRunSpecific + 409
    14  GraphicsServices                    0x00000001112549c6 GSEventRunModal + 62
    15  UIKit                               0x000000010a4c85d6 UIApplicationMain + 159
    16  memeGenerator                       0x0000000108be5157 main + 55
    17  libdyld.dylib                       0x000000010eabdd81 start + 1
    18  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Here是回购的链接。当用户点击相应的行时,我试图让应用程序显示详细视图控制器和保存的meme图像。我哪里错了?

ios swift tableview didselectrowatindexpath
1个回答
1
投票

你有两个崩溃:第一个是因为你使用的故事板是错误的(DetailsCollectionViewCell没有在self.storyboard中定义),第二个是因为你试图在detailsImageView中设置尚未初始化的meme图像,所以你可以改变你的didSelectRowAt in这条路:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let meme = memes[indexPath.row]
    let detailController = UIStoryboard(name: "DetailsStoryboard", bundle: nil).instantiateViewController(withIdentifier: "DetailsCollectionViewCell") as! DetailsCollectionViewCell
    present(detailController, animated: true, completion: nil)
    detailController.detailsImageView.image = meme.memedImage
}
© www.soinside.com 2019 - 2024. All rights reserved.