【问题标题】:UITableView background is visible under cell when row actions are shown显示行操作时,UITableView 背景在单元格下可见
【发布时间】:2017-04-19 07:04:33
【问题描述】:

我有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];

但是效果并不好。

还有其他方法可以“隐藏”单元格及其编辑操作之间的空白吗?

【问题讨论】:

标签: ios uitableview


【解决方案1】:

我设法通过在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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多