大佬教程收集整理的这篇文章主要介绍了objective-c – 继续从dequeueReusableCellWithIdentifier获取nil?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath { static NSString* MAINVIEW_CELLIDENTIFIER = @"mainViewTableCell"; UITableViewCell *newCell = [tableView dequeueReusableCellWithIdentifier: MAINVIEW_CELLIDENTIFIER]; if (newCell == nil) { newCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reusEIDentifier: MAINVIEW_CELLIDENTIFIER]; [newCell autorelease]; newCell.SELEctionStyle = UITableViewCellSELEctionStyleNone; } NTContactItem* currentItem = [self.contactItemContainer objecTinContainerATindex: indexPath.row]; NSString* firstName = currentItem.firstName; NSString* lastName = currentItem.lastName; NSString* fullName = [firstName StringByAppendingFormat: lastName]; [newCell.textLabel setText: fullName]; [newCell.detailTextLabel setText: currentItem.mobilePhone]; return newCell; }
但我继续从dequeueReusableCellWithIdentifier获取nil,并且每次都必须创建一个新的cell实例.
那么,有什么不对?
谢谢大家.
在你的情况下,问题是完全不同的(我下载你的项目,因为我真的很好奇).
在应用程序的应用程序委托中:didFinishLaunchingWithOptions:方法,您将重新初始化此tableviewcontroller.当你调用[masterController init]时,这会调用[super init],然后调用[UITableViewController initWithStyle:].
这会导致控制器创建一个新的UITableView,它与故事板中的UITableView不同.新的UITableView没有原型单元格,这就是为什么dequeueReusableCellWithIdentifier:返回nil.
当然,教训是不重新初始化已经初始化的Objective-C对象.从故事板加载表视图控制器时,加载机制将使用initWithCoder:初始化它.因此,如果您需要进行一些自定义初始化工作(比如在您的情况下设置NSMutableArray),那么只需覆盖initWithCoder:和/或awakeFromNib.
您可以根据需要覆盖这些方法,但不要自己调用它们. initWithCoder:和awakeFromNib都将由Storyboard / nib加载机制调用.
如果一切正确,则无需在此以编程方式创建单元格.不需要这段代码:
// This bit is unnecessary with storyboards: if (newCell == nil) { newCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reusEIDentifier: MAINVIEW_CELLIDENTIFIER]; [newCell autorelease]; newCell.SELEctionStyle = UITableViewCellSELEctionStyleNone; }
希望有所帮助.
以上是大佬教程为你收集整理的objective-c – 继续从dequeueReusableCellWithIdentifier获取nil?全部内容,希望文章能够帮你解决objective-c – 继续从dequeueReusableCellWithIdentifier获取nil?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。