(1)dequeueReusableCellWithIdentifier: forIndex: 方法是IOS6.0+版本才出现的,新的重用机制方法;只能在IOS 6.0以上系统才有效;
注意官方SDK说明: newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
即:使用dequeueReusableCellWithIdentifier: forIndex: 必须使用配套的
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
或者
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
```
- 这个方法。对每一个tableview的cell进行注册。
1,使用dequeueReusableCellWithIdentifier:CELL_INDENTIFIER 后面cell需要判断为nil时候,生成一个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* CELL_INDENTIFIER=@"IPHONE_LIST_CELL";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: CELL_INDENTIFIER];
```
//cell最初从cell队列中获取,为nil。并不存在
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CELL_INDENTIFIER];
//设置你的cell内容
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
}
return cell;
}
2,用新方法的写法:UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell" forIndexPath:indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_INDENTIFIER forIndexPath:indexPath];
//cell 从队列中获取得到,不会为nil 原因就是在初始化tablview的时候已经用到
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"IPHONE_LIST_CELL"];进行了注册。
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
return cell;
}
注明:使用2的新方式重用机制,如果没有对tableview
registerClass注册cell的话,会闪退报错:
must register a nib or a class for the identifier or connect a prototype cell in a storyboard’