UITableView 分隔线在滚动时单元格之间消失

发布于 2024-08-06 15:29:32 字数 345 浏览 1 评论 0原文

问题:表视图中的单元格之间的分隔符仅针对视图加载时显示的单元格显示,并且仅在加载时显示。当表格视图向下滚动时,滚动到视图中的单元格之间不显示分隔符,然后当表格视图向上滚动时,初始单元格不显示分隔符。

详细信息:我有一个 UITableView,我要向其中添加标准 UITableViewCells。这些单元格是使用 initWithFrame 创建的,框架高度 = 90px。我将从笔尖创建的自定义视图添加到此单元格的视图中,高度 = 90px。单元格高度在 tableView:heightForRowAtIndexPath: 中指定为 90px。

有人经历过这种行为吗?

Problem: The separator between cells in a table view appear only for those cells shown when the view loads, and only at load time. When the tableview is scrolled down, the cells scrolled into view show no separator between them, then when the tableview is scrolled back up, the initial cells show no separator.

Details: I've got a UITableView to which I'm adding standard UITableViewCells. These cells are created with initWithFrame, frame height = 90px. I'm adding a custom view created from a nib to this cell's view, height = 90px. The cell height is specified at 90px in tableView:heightForRowAtIndexPath:.

Has anyone experienced this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云巢 2024-08-13 15:29:32

我有一种感觉,解决这个问题很简单......
我将单元格的高度设置为 91px,并且分隔线按照滚动时的样子显示。

I had a feeling the solution to this would be simple...
I made the height of my cells 91px and the separator lines appear as they should on scroll.

糖粟与秋泊 2024-08-13 15:29:32

我无法使用道格拉斯的解决方案,因为我的桌子有大量的单元格,并且在旧手机上几乎无法使用。重复使用单元是性能的关键。

但是,我设法使用透明分隔符并在单元格的 contentView 中添加我自己的分隔符来解决该问题,如下所示:

yourTable.separatorColor = [UIColor clearColor];
separatorView.frame = FactRectMake(0, rowHeight-1, appFrame.size.width, 0.2);

I couldn't use Douglas's solution because my tables have a huge amount of cells and would become pretty much unusable on older phone. Reusing cells is key for performance.

BUT, I managed to workaround the problem using a transparent separator and adding my own in the contentView of the cell, as follows:

yourTable.separatorColor = [UIColor clearColor];
separatorView.frame = FactRectMake(0, rowHeight-1, appFrame.size.width, 0.2);
静若繁花 2024-08-13 15:29:32

我有同样的问题,但我使用了不同的解决方案。
我的分隔符消失了,因为我正在使用以下方法清除单元格:

for (UIView *eachView in self.subviews) {
    [eachView removeFromSuperview];
}

这也删除了分隔符视图!

相反,我在将每个自定义视图添加到子视图之前为它们分配了一个标签(三个标签):

tempFirstNameLabel.tag = 100;
self.firstNameLabel = tempFirstNameLabel;
[self addSubview:self.firstNameLabel];

然后,当我清除单元格时,我刚刚删除了这些视图:

for (int i = 100; i<103; i++) {
    UIView *eachView = [self viewWithTag:i];
    [eachView removeFromSuperview];
}

希望这有帮助!
这也避免了@Douglas Smith 的解决方案带来的内存管理问题。

I had the same problem, but I used a different solution.
My separators were disappearing because I was clearing my cell using:

for (UIView *eachView in self.subviews) {
    [eachView removeFromSuperview];
}

This removed the separator view as well!

Instead, I assigned a tag for each of my customs views (three labels) right before adding them to the sub view:

tempFirstNameLabel.tag = 100;
self.firstNameLabel = tempFirstNameLabel;
[self addSubview:self.firstNameLabel];

Then when I cleared the cell, I just removed those views:

for (int i = 100; i<103; i++) {
    UIView *eachView = [self viewWithTag:i];
    [eachView removeFromSuperview];
}

Hope this helps!
This also avoids the memory management issues that @Douglas Smith's solution posed.

很酷不放纵 2024-08-13 15:29:32

您应该将分隔符设置为无,然后再次设置单行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // it is a bug in iOS 7
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

You should set separator none and then single line again

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // it is a bug in iOS 7
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文