UITableViewCell Storyboard加載錯誤解決

1. 當使用XIB文件時,使用Cell前需要在ViewDidLoad方法中註冊

UINib *nib = [UINib nibWithNibName:CLASS_NAME bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:CELL_IDENTIFIER_IN_XIB];
 

然後在cellForRowAtIndexPath中使用:

NewProductCell *cell = [self.tableView dequeueReusableCellWithIdentifier:<span style="font-family: Arial, Helvetica, sans-serif;">CELL_IDENTIFIER_IN_XIB </span><span style="font-family: Arial, Helvetica, sans-serif;">forIndexPath:indexPath];</span>

2. 當使用StoryBoard時,使用1的方法會出現錯誤:

NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/XXX/Library/Developer/CoreSimulator/Devices/2201F07B-8301-4164-8FE0-26EA82D9895C/data/Containers/Bundle/Application/79E7DC2C-ED24-4031-9AAC-10BE9BC9955D/xpyx.app> (loaded)' with name 'NewProductCell'

解決方法是把ViewDidLoad中的註冊部分刪除掉,原因估計是在初始化ViewController時,已經自動註冊了關聯的Cell。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章