我在 http://news.selectstartstudios.com/beveled-uitableviewcells/ 找到了关于 Beveled UITableViewCells 的这篇文章。我正在使用该技术来减小单元格的宽度,并且在大多数情况下它效果很好。
但是,我有一个小问题。有时单元格未正确重绘。例如,即使单元格应该是“中间”单元格,它也会被绘制为“顶部”单元格:yfrog.com/f1screenshot20100424at100
我该如何解决这个问题?
我尝试通过 [cell setNeedsDisplay]、[cell setNeedsLayout]、[tableView reloadRowAtIndexPath:withAnimation:]、[cell drawrect:] 和 [tableView drawRect:atIndexPath] 强制单元格重绘。我没有主意了。
再次感谢!
结果我需要在 cellForRowAtIndexpath 的末尾调用 [cell.backgroundView setNeedsDisplay] 。
遇到了同样的问题,因为我的一些
cellForRowAtIndexpath
位于后台线程中。这种情况下的解决方案是强制将任何直接单元格子视图更新到主线程中:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
if (cell.tag == indexPath.row){
// data retrieval that may block main thread and cause temp freeze...
dispatch_async(dispatch_get_main_queue(), ^{
// update any cell subviews here
}
}
});
有一个
UITableViewCell
- prepareForReuse()
的方法:
https://developer.apple.com/documentation/uikit/uitableviewcell/prepareforreuse()
例如,在我的例子中,我有一个单元格子视图,我只需要重绘它,而不是整个单元格:
override func prepareForReuse() {
super.prepareForReuse()
someInnerView.setNeedsDisplay()
}
它比
DispatchQueue
的奇怪答案更好,也比 cellForRowAtIndexpath
的答案更好,因为您可以将它与任何单元格的子视图(甚至是私有的)一起使用。