iOS高效开发者iOS Developer

解决UITableView复用

2017-06-27  本文已影响42人  小熊翻译App

解决复用的写法1:

// 通过indexPath创建cell实例 每一个cell都是单独的
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        }

解决复用的写法2:

// 定义cell标识  每个cell对应一个自己的标识
        NSString *cellId = [NSString stringWithFormat:@"cell%zd%zd",indexPath.section,indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        }
上一篇 下一篇

猜你喜欢

热点阅读