iOS学习——UITableViewCell两种重用方法的区别

  今天在开发过程中用到了UITableView,在对cell进行设置的时候,我发现对UITableViewCell的重用设置的方法有如下两种,刚开始我也不太清楚这两种之间有什么区别。直到我在使用方法二进行重用的时候,具体实现代码如下,其中CJMeetingReplyBasicCell是我自定义的UITableViewCell类型,但是在运行的时候每次都在调用 CJMeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath]; 时崩溃,通过查找各种原因,确定不是自己代码的问题之后,开始了解这两种重用方法的区别。那么,这两种重用UITableViewCell的方法之间到底有什么区别呢?

//方法一
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//方法二
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"];
    }
    
    return cell;
}

一 官网文档解释

  首先我们看一下在iOS源码的UITableView.h中对两者的解释如下,我们可以看到方法二是在iOS 6.0中开始推出的新方法,在对方法二的解释中,我们注意标红的部分的意思是假设我们已经注册了标识符,这里我们猜测可能是我们需要对标识符进行注册。

//UITableView.h
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

  接下来我们找到官方文档,https://developer.apple.com/documentation/uikit/uitableview/1614878-dequeuereusablecellwithidentifie?language=objc,官方文档对方法二的解释有两点需要注意,第一个是返回值的地方如下图,这里说道该方法总是返回一个有效的UITableViewCell,这是与方法一不同的地方之一。

  第二个需要注意的地方是,在该页面下面有一个Important的提示如下图,该提示就说明了方法二的正确使用方法。这里解释说要先进行注册我们自定义或者通过nib的类和标识符,然后再使用方法二进行重用。所以现在我们崩溃的原因就已经明确了,问题就出在没有进行先注册我们自定义的类和标识符。

二 常规使用方法

  对于这两种方法的常规使用方法,下面进行总结一下。

  首先,对于方法一,使用方法很简单,无需进行其他的定义和注册,代码如下。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell"];
    if (!cell) {
        cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"];
    }
    
    return cell;
}

  接下来,我们简单使用方法二进行重用,具体步骤代码如下。

//首先,我们再tableview进行配置的时候需要注册我们已定义的cell类和重用标识符
self.tableView.backgroundColor = xxxx;
[self.tableView registerClass:[MeetingReplyBasicCell class] forCellReuseIdentifier:@"BasicCell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
    
    return cell;
}

 

posted on 2018-03-16 16:25  mukekeheart  阅读(2080)  评论(1编辑  收藏  举报