UITableView mingles cell background colors on fast scrolling

前端 未结 2 1720
眼角桃花
眼角桃花 2021-01-27 11:24

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: inst

相关标签:
2条回答
  • 2021-01-27 12:14

    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];
    }
    
    0 讨论(0)
  • 2021-01-27 12:27

    The reason is that you are not dealing with the fact that cells are reused. You are providing instructions for half the cases:

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

    But you are not saying what to do in the other half of the cases.

    This has nothing to do with speed of scrolling. It's just that the very same cell that you previous set to gray will later be used in a row that you want to be white. So you need to set it to white, or it will still be gray from its previous use. This is true of every aspect of a cell.

    0 讨论(0)
提交回复
热议问题