2013-10-11 13:48:58| 分类: iphone开发 | 标签:utableview ios6 storyboard |举报|字号 订阅
近来经常看到朋友在使用最新的iOS SDK 6.0版本的UITabelView的时候,会出现以下的错误:
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard‘
这个是什么错误呢?我们首先从字面上理解它,“must register a nib or a class for the identifier or connect a prototype cell in a storyboard”,这是说标记为Cell的UITableViewCell不能出列,你必须为这个Cell注册一个Class或从storyboard连接一个Cell(英文水平不怎么样,将就一下)。OK,我们到这个方法的文档里去一探究竟。
我们看这个方法的注释
NS_AVAILABLE_IOS(6_0);
// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
1 这是一个6.0才能使用的方法。2 获得一个Cell,假设标记的这个Cell已经被注册。
我们再往下看,下面还有两个配套的方法:
// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
从IOS6开始,客户端可以为每一个cell注册nib或class如果所有的重用标记都被注册了,就可以使用新方法 -dequeueReusableCellWithIdentifier:forIndexPath:来获得一个cell实例。
意即说如果要使用新方法 -dequeueReusableCellWithIdentifier:forIndexPath:就必须使用配套的
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier 或者 - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier这个方法。
我们之前为什么会出现那样的错误,现在应该有点眉目了。出错的情况如下:
1 编译的设备的系统低于6.0版本
2 未使用配套的注册方法
下面我来演示一下使用新方法来生成一个tableview
1 新建一个Single View Application.
2 新建一个MXCustomCell子类。如图:
3 现在项目结构如下:
4 打开ViewController.h,添加UITableViewDatasource,UITableViewDelegate协议。如图:
5 打开ViewController.m,导入MXCustomCell.h,并声明一个CellIdentifier,如下:
7 声明一个UITableView属性:
@property (retain,nonatomic) UITableView *notNib_tableView;
8 在viewDidLoad方法里添加下列语句:
10 OK,完成,我编译运行一下看看。
OK,现在应该有点眉目了吧,所以说在我们使用新SDK的时候,如果出现上述错误,一般是以下的两种情况:
1 编译的设备的系统版本低于6.0
2 没有对dequeueReusableCellWithIdentifier: forIndexPath:使用配套的注册cell的方法
PS:如果我不需要自定义Cell的时候怎么办呢,也方便,只需要
[self.notNib_tableViewregisterClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];方法中的registerClass参数传一个[UITableViewCell class]过去就行了,这样就使用系统默认的UITableViewCell了。
【转】iOS6 之后的tableView的改动,布布扣,bubuko.com
原文:http://www.cnblogs.com/lixiong-iOS/p/3613924.html