gpt4 book ai didi

ios - 无法通过 didSelectRow tableView 方法显示 View Controller

转载 作者:行者123 更新时间:2023-11-28 06:06:29 25 4
gpt4 key购买 nike

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

这是来 self 的 TableView Controller 的代码:

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)
}

}

这是我的详细信息 View Controller 中的代码:

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是 repo 的链接。当用户点击相应的行时,我试图让应用程序显示带有保存的模因图像的详细信息 View Controller 。我哪里错了?

最佳答案

您有两次崩溃:第一次是因为您使用的 Storyboard是错误的(DetailsCollectionViewCell 未在 self.storyboard 中定义),第二次是因为您正在尝试在尚未初始化的 detailsImageView 中设置 meme 图像,因此您可以这样更改 didSelectRowAt:

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
}

关于ios - 无法通过 didSelectRow tableView 方法显示 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com