简而言之,我需要恢复单元格的默认backgroundView
值UITableViewStyleGrouped
。
cell.backgroundView = ??;
长话短说:我有一个带有一些单元格的分组表格视图。对于每个单元格,用户需要选择一些值或进行一些输入。之后,他可以通过触摸“下一步”按钮进入下一个 ViewController。
无论如何,为了向用户表明他错过了一些东西,我在单元格周围显示了一个漂亮的红色边框,其中包含错误/丢失的用户输入。我通过backgroudView
使用这样的自定义图像设置单元格来做到这一点:
cell.backgroundView = myErrorIndicatingImageView;
单元格现在看起来像这样(截图,忽略星星和标签)
到目前为止一切顺利,这就像一个魅力。但是在用户更正输入后,我想删除我的红色边框背景图像并再次显示原始背景。原始背景如下所示(截图):
这似乎是一个问题。
我尝试了几件事
// try by setting the background to nil
cell.backgroundView = nil;
这完全消除了背景,我迷失了一个没有背景的单元格。
// try it with addSubview and later remove the subview again
[cell.backgroundView addSubview:myErrorIndicatingImageView];
这没有任何作用。myErrorIndicatingImageView
是不可见的。连一个[cell.backgroundView bringSubviewToFront:myErrorIndicatingImageView]
也无济于事。
现在我唯一的解决方案是UITableViewCell
为每次调用创建一个新的cellForRowAtIndexPath
. 这有效,但只是糟糕的代码,完全忽略了重用单元的技术。
必须有一个更简单的解决方案......就像
cell.backgroundView = [self.tableView getDefaultBackgroundView];