天天看點

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。