蛋疼的cell 重用问题

以前也整过tableView ,但从来没被cell 的重用问题搞晕过头,可能是因为写的table太简单了吧。这次是重用各种出问题,以至于都没信心写tableView 了。

首先得弄清楚cell的重用是怎么一回事,TableView的重用机制,为了做到显示和数据分离,IOS tableView的实现并且不是为每个数据项创建一个tableCell。而是只创建屏幕可显示最大个数的cell,然后重复使用这些cell,对cell做单独的显示配置,来达到既不影响显示效果,又能充分节约内容的目的。TableView显示之初,reusableTableCells为空,那么tableView dequeueReusableCellWithIdentifier:CellIdentifier返回nil。开始的cell都是通过[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier]来创建,而且cellForRowAtIndexPath只是调用最大显示cell数的次数。


比如:有100条数据,iPhone一屏最多显示10个cell。程序最开始显示TableView的情况是:

 1. 用[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]创建10次cell,并给cell指定同样的重用标识(当然,可以为不同显示类型的cell指定不同的标识)。并且10个cell全部都加入到visiableCells数组,reusableTableCells为空。


 2. 向下拖动tableView,当cell1完全移出屏幕,并且cell11(它也是alloc出来的,原因同上)完全显示出来的时候。cell11加入到visiableCells,cell1移出visiableCells,cell1加入到reusableTableCells。


  3. 接着向下拖动tableView,因为reusableTableCells中已经有值,所以,当需要显示新的cell,cellForRowAtIndexPath再次被调用的时候,tableView dequeueReusableCellWithIdentifier:CellIdentifier,返回cell1。cell1加入到visiableCells,cell1移出reusableTableCells;cell2移出visiableCells,cell2加入到reusableTableCells。之后再需要显示的Cell就可以正常重用了。


  所以整个过程并不难理解,但需要注意正是因为这样的原因:配置Cell的时候一定要注意,对取出的重用的cell做重新赋值,不要遗留老数据。

明白了cell 的重用机制,所以我们在遇到这个问题的时候就有很好的解决办法了,我的解决办法是不同的cell 利用不同的标示符,上代码举例子。





图片上的问题明显都是cell的重用用错了。下面看我的代码如何解决了这一问题。

 int section = indexPath.section;
    int row = indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (section == 0) {
        
        static NSString *cellfirstID = @"cellfirstID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellfirstID];
        if (cell == nil)
        {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellfirstID];
            
        }
        if (row == 0)
        {
            
            smallBtn = [UIButton buttonWithTitle:@"小" titleColor:[UIColor lightGrayColor] backgroundColor:[UIColor whiteColor] target:self action:@selector(smallAction:)];
            [smallBtn setBackgroundImage:[UIImage imageNamed:@"bg1_size_img"] forState:UIControlStateNormal];
            [smallBtn setBackgroundImage:[UIImage imageNamed:@"bg2_size_img"] forState:UIControlStateHighlighted];
            [smallBtn setBackgroundImage:[UIImage imageNamed:@"bg2_size_img"] forState:UIControlStateSelected];
            [smallBtn setFrame:CGRectMake(190, 15, 40, 28)];
            [cell addSubview:smallBtn];
            middleBtn = [UIButton buttonWithTitle:@"中" titleColor:[UIColor lightGrayColor] backgroundColor:[UIColor whiteColor] target:self action:@selector(middleAction:)];
            [middleBtn setBackgroundImage:[UIImage imageNamed:@"bg1_size_img"] forState:UIControlStateNormal];
            [middleBtn setFrame:CGRectMake(230, 15, 40, 28)];
            [middleBtn setBackgroundImage:[UIImage imageNamed:@"bg1_size_img"] forState:UIControlStateNormal];
            [middleBtn setBackgroundImage:[UIImage imageNamed:@"bg2_size_img"] forState:UIControlStateHighlighted];
            [middleBtn setBackgroundImage:[UIImage imageNamed:@"bg2_size_img"] forState:UIControlStateSelected];
            [cell addSubview:middleBtn];
            
            bigBtn  = [UIButton buttonWithTitle:@"大" titleColor:[UIColor lightGrayColor] backgroundColor:[UIColor whiteColor] target:self action:@selector(bigAction:)];
            [bigBtn setFrame:CGRectMake(270, 15, 40, 28)];
            [bigBtn setBackgroundImage:[UIImage imageNamed:@"bg1_size_img"] forState:UIControlStateNormal];
            [bigBtn setBackgroundImage:[UIImage imageNamed:@"bg2_size_img"] forState:UIControlStateHighlighted];
            [bigBtn setBackgroundImage:[UIImage imageNamed:@"bg2_size_img"] forState:UIControlStateSelected];
            [cell addSubview:bigBtn];
            cell.textLabel.text = @"字体大小";
        }

else if (section == 1)
        {
            static NSString *cellSecondID = @"cellSecondID";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSecondID];
            if (cell == nil) {
                
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellSecondID];
            }
            if (row == 0) {
                cell.textLabel.text = @"清楚缓存";
            }
            if (row == 1) {
                cell.textLabel.text = @"推荐给朋友";
            }
            if (row == 2) {
                cell.textLabel.text = @"帮助";
            }
            if (row == 3) {
                cell.textLabel.text = @"版本更新";
            }
            if (row == 4) {
                cell.textLabel.text =@"关于我们";
            }
            return cell;
    }
    return cell;

完美解决,如果你有好的解决方案,也可以给我说一下,欢迎交流。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章