I have a variable that is going to keep track of how many cells need to be colored. So if that variable is 3, then the top three cells backgroundcolor will change. How can I do this?
I know I need to update this in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But how can I ensure the top cells have a different background color based on my variable?
The
indexPathparameter is your starting point. IfcoloredCellsis an integer that holds the number of cells you are coloring, your method would include something likeNow, if you adjust the value of
coloredCells, you’ll need to inform the table view that some of its views have changed. The laziest way to do that is to reload the whole table:Or you can take a little more effort to reload just the cells that have colored backgrounds: