Adding a subview larger than cellHeight to a UITableViewCell?

后端 未结 4 1667
情歌与酒
情歌与酒 2020-12-30 04:45

I\'m trying to add a subview to a UITableViewCell and the design that I\'m working from demands that this particular subview (an image) needs to be larger than the actual UI

4条回答
  •  粉色の甜心
    2020-12-30 05:18

    I seems that the tableView renders its cell from bottom to top, so the cells above one cell overlap that one cell. To avoid this, you'd have to set the backgroundColor of all cells to +[UIColor clearColor] so that you won't see those overlap problems.

    But setting the backgroundColor to clear in -tableView:cellForRowAtIndexPath: does not make any sense. UIKit does a lot of stuff with the cell before it's drawn, so does it reset the backgroundColor property of the cell.

    What we need to do is setting the backgroundColor in a later state. Luckily there is this -[UITableViewDelegate tableView:willDisplayCell:forRowAtIndexPath:] which we can implement like this:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        cell.backgroundColor = [UIColor clearColor];
    }
    

    Now we're setting the backgroundColor just before the cell is drawn an this turns out to be working.

提交回复
热议问题