UITableView mingles cell background colors on fast scrolling

前端 未结 2 1724
眼角桃花
眼角桃花 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];
    }
    

提交回复
热议问题