UIEdgeInsetsMake
将我的单元格背景设置为渐变色。我尝试了多种方法来使其工作,但无论我使用什么,总是会有问题。我只有两个静态单元格,我想在
willDisplayCell:
中设置它们的 backgroundView
。我为顶部、底部和中间单元格分别准备了独立的图像,但由于我只有两个单元格,因此我只需要顶部和底部的图像。这些是那些图片:
顶部
![enter image description here](https://istack.dev59.com/MakQT.webp)
![enter image description here](https://istack.dev59.com/d1PHM.webp)
我按以下方式设置它们:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIImageView *topCellBackgroundImageView =
[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg-top"]
resizableImageWithCapInsets:UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0)]];
cell.backgroundView = topCellBackgroundImageView;
}
// If it's the last row
else if (indexPath.row == ([tableView numberOfRowsInSection:0] - 1)) {
UIImageView *bottomCellBackgroundImageView =
[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg-bottom"]
resizableImageWithCapInsets:UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0)]];
cell.backgroundView = bottomCellBackgroundImageView;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
这并不有效。如下图所示,在顶部单元格中,有一个1pt的白色“带子”穿过整个单元格,看起来非常难看。我不知道它为什么会出现。
因此,我将topCellBackgroundView
的边缘插入更改为(5.0, 5.0, 0.0, 5.0)
,因为它只在顶部是圆角的,所以无需考虑bottom
属性(它是平的)。这样做完美地解决了问题!但是当你选择该单元格时,底部单元格不再占据整个单元格。
我该怎么办?似乎无论我做什么,都不起作用。我还尝试过使用1.0代替0.0,以及-1.0,但都没有效果。