无法通过 didSelectRow tableView 方法显示视图控制器
Cannot display view controller via didSelectRow tableView method
我正在构建一个模因生成器应用程序。我有一个模因生成器屏幕,它由任一选项卡栏视图控制器屏幕内的导航栏按钮项调用。第一个标签栏屏幕在 table 视图中显示保存的模因,第二个在集合视图中显示保存的模因。我正在尝试添加一个详细信息视图控制器,当用户点击 table 视图控制器上的行时显示保存的模因,目前,我的应用程序在触摸事件时崩溃。
这是来自我的 table 视图控制器的代码:
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 是回购的 link。当用户点击相应的行时,我试图让应用程序显示带有保存的模因图像的详细信息视图控制器。我哪里错了?
你有两个崩溃:第一个是因为你使用的故事板是错误的(DetailsCollectionViewCell
没有在 self.storyboard
中定义),第二个是因为你试图在 [= =13=] 尚未初始化,因此您可以这样更改 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
}
我正在构建一个模因生成器应用程序。我有一个模因生成器屏幕,它由任一选项卡栏视图控制器屏幕内的导航栏按钮项调用。第一个标签栏屏幕在 table 视图中显示保存的模因,第二个在集合视图中显示保存的模因。我正在尝试添加一个详细信息视图控制器,当用户点击 table 视图控制器上的行时显示保存的模因,目前,我的应用程序在触摸事件时崩溃。
这是来自我的 table 视图控制器的代码:
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 是回购的 link。当用户点击相应的行时,我试图让应用程序显示带有保存的模因图像的详细信息视图控制器。我哪里错了?
你有两个崩溃:第一个是因为你使用的故事板是错误的(DetailsCollectionViewCell
没有在 self.storyboard
中定义),第二个是因为你试图在 [= =13=] 尚未初始化,因此您可以这样更改 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
}