首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当显示行操作时,UITableView背景在单元格下可见。

当显示行操作时,UITableView背景在单元格下可见。
EN

Stack Overflow用户
提问于 2017-04-19 07:04:33
回答 1查看 582关注 0票数 0

我的UITableView有白色的部分标题和黑色的单元格。桌子的背景也是白色的。这样做的目的是在表跳时使节标题看起来很好看:

我向这个表中的行添加了几个操作(UITableViewRowAction)。但是,有一个问题:由于行是暗的,所以当我滑动以显示行编辑操作时,第一个操作之间有一个空白:

(请注意,单元格及其内容视图在XIB文件中都设置了相同的深色背景色。)

在检查视图层次结构之后,我发现在显示操作按钮之后,单元格被缩小了,而我通过这个空白看到的是实际的表视图。

由于无法更改表视图的背景,所以我尝试在所有其他视图下面添加一个自定义背景视图:

代码语言:javascript
运行
复制
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];

然而,这并不是很好。

还有其他方法来“隐藏”单元格和它们的编辑操作之间的空白吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-25 07:33:36

我设法克服了这一点,在tableView:willDisplayCell:内部的每个单元后面添加了一个背景(每个单元格一个)。

这是我的最后代码:

代码语言:javascript
运行
复制
- (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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43488732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档