Swift UITableView 无法识别的选择器发送到实例
Posted
技术标签:
【中文标题】Swift UITableView 无法识别的选择器发送到实例【英文标题】:Swift UITableView unrecognized selector sent to instance 【发布时间】:2015-07-15 15:31:20 【问题描述】:在我的选项卡式应用程序中,第一个选项卡是一个导航控制器,它显示聊天会话的表格视图 (ChatsViewController)。在视图控制器中,因为我使用 Parse 作为后端,所以我测试 PFUser.currentUser() 是否为 nil,如果是,则我提供一个身份验证视图控制器。但是,打开应用程序后,它会在启动屏幕后立即崩溃。这是控制台输出:
1
2
2015-07-15 11:13:55.251 Chat App[1116:90011] Presenting view controllers on detached view controllers is discouraged <Chat_App.ChatsViewController: 0x7fade2442fa0>.
3
2015-07-15 11:13:55.256 Chat App[1116:90011] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fade262ec70
2015-07-15 11:13:55.264 Chat App[1116:90011] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fade262ec70'
*** First throw call stack:
(
0 CoreFoundation 0x0000000106a3bc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001085a6bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000106a430ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010699913c ___forwarding___ + 988
4 CoreFoundation 0x0000000106998cd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000107564252 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2353
6 UIKit 0x0000000107569edc -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 324
7 UIKit 0x000000010756a020 -[UITableViewRowData heightForTable] + 56
8 UIKit 0x00000001073b3720 -[UITableView _updateContentSize] + 381
9 UIKit 0x00000001073d34b2 -[UITableView _rebuildGeometry] + 40
10 UIKit 0x00000001073d18b0 -[UITableView didMoveToWindow] + 104
11 UIKit 0x0000000107355a28 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1496
12 UIKit 0x000000010736748b -[UIScrollView _didMoveFromWindow:toWindow:] + 55
13 UIKit 0x000000010735570a -[UIView(Internal) _didMoveFromWindow:toWindow:] + 698
14 UIKit 0x000000010735570a -[UIView(Internal) _didMoveFromWindow:toWindow:] + 698
15 UIKit 0x000000010734de8f __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
16 UIKit 0x000000010734de03 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
17 UIKit 0x0000000107357f08 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1660
18 UIKit 0x000000010763e80c -[UINavigationTransitionView transition:fromView:toView:] + 479
19 UIKit 0x000000010743c170 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2984
20 UIKit 0x000000010743c408 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
21 UIKit 0x000000010743cece -[UINavigationController __viewWillLayoutSubviews] + 43
22 UIKit 0x00000001075876d5 -[UILayoutContainerView layoutSubviews] + 202
23 UIKit 0x000000010735a9eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
24 QuartzCore 0x000000010663bed2 -[CALayer layoutSublayers] + 146
25 QuartzCore 0x00000001066306e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
26 QuartzCore 0x0000000106630556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
27 QuartzCore 0x000000010659c86e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
28 QuartzCore 0x000000010659da22 _ZN2CA11Transaction6commitEv + 462
29 UIKit 0x00000001072d89ed -[UIApplication _reportMainSceneUpdateFinished:] + 44
30 UIKit 0x00000001072d96b1 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2648
31 UIKit 0x00000001072d8095 -[UIApplication workspaceDidEndTransaction:] + 179
32 FrontBoardServices 0x000000010afb55e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
33 CoreFoundation 0x000000010696f41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
34 CoreFoundation 0x0000000106965165 __CFRunLoopDoBlocks + 341
35 CoreFoundation 0x0000000106964f25 __CFRunLoopRun + 2389
36 CoreFoundation 0x0000000106964366 CFRunLoopRunSpecific + 470
37 UIKit 0x00000001072d7b02 -[UIApplication _run] + 413
38 UIKit 0x00000001072da8c0 UIApplicationMain + 1282
39 Chat App 0x00000001054ca787 main + 135
40 libdyld.dylib 0x000000010925f145 start + 1
41 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
这是 ChatsViewController 的代码:
class ChatsViewController: UIViewController, UITableViewDataSource
@IBOutlet var chatsTable: UITableView!
var chats: [String] = []
override func viewDidLoad()
super.viewDidLoad()
if PFUser.currentUser() == nil
println(1)
let authVC: AuthenticationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("auth") as! AuthenticationViewController // a custom class for the authentication view
println(2)
self.presentViewController(authVC, animated: true, completion: nil)
println(3)
else
chatsTable.dataSource = self
chats = PFUser.currentUser()!.objectForKey("chats") as! [String]
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return chats.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell: ChatSessionCell = chatsTable.dequeueReusableCellWithIdentifier("ChatSessionCell", forIndexPath: indexPath) as! ChatSessionCell
var chatID = chats[indexPath.row]
var chat = getChatWithID(chatID)
// TODO: put content in cell
return cell
func getChatWithID(id: String) -> PFObject
var query = PFQuery(className: "Chat")
query.whereKey("objectID", equalTo: id)
let object: [PFObject] = query.findObjects() as! [PFObject] // should only be one object
return object.first!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "toSession"
var sessionVC: SessionViewController = segue.destinationViewController as! SessionViewController
sessionVC.sessionID = chats[chatsTable.indexPathForSelectedRow()!.row]
我已经搜索过这个错误,但似乎对于每种情况,原因和解决方案都不同。我该如何解决这个问题?
【问题讨论】:
您有当前用户吗?你确定数据源设置了吗? 当前没有用户,我试着打印出来,结果打印为零。此外,数据源在代码中设置(在 else 块中)并在情节提要中连接。 【参考方案1】:已解决:我将 chatsTable.dataSource = self
移到 if 语句之前,这解决了问题。很可能它找不到 numberOfRowsInSection 方法,因为只有在存在 currentUser 时才设置 dataSource。
【讨论】:
【参考方案2】:您的视图控制器在 chats
实例化之前调用了 numberOfRowsInSection
。
您应该将var chats: [String]!
替换为var chats = [String]()
【讨论】:
在到达chats.count
之前发生错误。日志说找不到numberOfRowsInSection
方法。以上是关于Swift UITableView 无法识别的选择器发送到实例的主要内容,如果未能解决你的问题,请参考以下文章
UITableview NSarray isEqualToString 无法识别的选择器发送到实例
无法识别的选择器发送到实例 - UITableView 和 Restkit LoadObjects 选项