我的UITableView
有白色的部分标题和黑色的单元格。桌子的背景也是白色的。这样做的目的是在表跳时使节标题看起来很好看:
我向这个表中的行添加了几个操作(UITableViewRowAction
)。但是,有一个问题:由于行是暗的,所以当我滑动以显示行编辑操作时,第一个操作之间有一个空白:
(请注意,单元格及其内容视图在XIB文件中都设置了相同的深色背景色。)
在检查视图层次结构之后,我发现在显示操作按钮之后,单元格被缩小了,而我通过这个空白看到的是实际的表视图。
由于无法更改表视图的背景,所以我尝试在所有其他视图下面添加一个自定义背景视图:
self.tableBackgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];
self.tableBackgroundView.backgroundColor = [UIColor redColor];
self.tableBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleSize;
[self.tableView addSubview:self.tableBackgroundView];
[self.tableView sendSubviewToBack:self.tableBackgroundView];
然而,这并不是很好。
还有其他方法来“隐藏”单元格和它们的编辑操作之间的空白吗?
发布于 2017-04-25 07:33:36
我设法克服了这一点,在tableView:willDisplayCell:
内部的每个单元后面添加了一个背景(每个单元格一个)。
这是我的最后代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
// Remove background gaps on the left and right sides of row separators on iOS < 10.
cell.backgroundColor = cell.contentView.backgroundColor;
// Remove another gap in row background when swiping in to the left to start editing.
CGRect cellFrame = [cell convertRect:cell.bounds toView:tableView];
UIView *backgroundView = self.cellBackgroundViews[indexPath];
if (backgroundView == nil) {
backgroundView = [[UIView alloc] init];
self.cellBackgroundViews[indexPath] = backgroundView;
}
backgroundView.frame = CGRectInset(cellFrame, 0, -1);
backgroundView.backgroundColor = cell.contentView.backgroundColor;
[self.tableView addSubview:backgroundView];
[self.tableView sendSubviewToBack:backgroundView];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
UIView *backgroundView = self.cellBackgroundViews[indexPath];
[backgroundView removeFromSuperview];
}
https://stackoverflow.com/questions/43488732
复制相似问题