UITableView在快速滚动时混合单元格背景颜色 [英] UITableView mingles cell background colors on fast scrolling

查看:57
本文介绍了UITableView在快速滚动时混合单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个IOS应用程序,当我尝试将单元格背景颜色设置为交替时,我们工作正常,当我慢慢滚动,但当我尝试快速滚动颜色混合:而不是白色灰色白色灰色;我得到灰色灰色白色灰色白色灰色灰色。

This is my first IOS app and when i try to set the cell background colors to alternate they work fine when i scroll slowly, but when i try to scroll fast the colors mingle: instead of white gray white gray; i get gray gray white gray white gray gray.

这是我使用的:

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath{

          if(indexPath.row % 2){
                cell.backgroundColor = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:250/255.0 alpha:1.0];
          }

    }

我还尝试将该代码放入在cellForRowAtIndexPath中,结果是一样的:(

I also tried to put that code in cellForRowAtIndexPath, but the result was the same:(

任何帮助都将不胜感激!

Any help would be greatly appreciated!

推荐答案

您需要添加一个else语句将背景颜色设置为白色。由于单元格重用,一旦单元格的背景设置为灰色,重复使用时它将永远不会被设置回白色对于不是行%2的行。如下所示:

You need to add an else statement to set the background color to white. Because of cell reuse, as soon as a cell's background is set to gray, it will never get set back to white back when reused for a row that isn't row % 2. Like so:

if(indexPath.row % 2){
    cell.backgroundColor = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:250/255.0 alpha:1.0];
} else {
    cell.backgroundColor = [UIColor whiteColor];
}

这篇关于UITableView在快速滚动时混合单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆