xib 创建tableviewCell的重用

有好多人说用xib创建的cell不再遵循重用机制,对此很不理解,经验证,即使是用xib创建的cell也是遵循重用机制的。注意的是:用xib创建cell的时候Identifier设定的与cellForRowAtIndexPath方法中的一致。

//cell中拖放一个Label,上面的文字是Label

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custTabCell"];

    if (!cell) {

        cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil]lastObject];

    }

    //验证

    //如果cell是从重用池中取出来的,那么test上面的文字就不是Label了,也就不再改变会一直重复循环

    if ([cell.test.text isEqualToString:@"Label"] ) {

        NSInteger i = indexPath.row;

        cell.test.text = [NSString stringWithFormat:@"%ld", (long)i];

    }

    return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 40;

}


猜你喜欢

转载自blog.csdn.net/qq_30126571/article/details/51280305
今日推荐