Tec*_*lor 1 uitableview ios swift
在我的选项卡式应用程序中,第一个选项卡是导航控制器,显示聊天会话的表格视图(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
Run Code Online (Sandbox Code Playgroud)
这是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]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我搜索过这个错误,但似乎对于每种情况,原因和解决方案都不同.我该如何解决这个问题?
已解决:我chatsTable.dataSource = self在if语句之前移动了,这解决了问题.很可能它找不到numberOfRowsInSection方法,因为只有当currentUser才设置dataSource.