我有一个UITableView
可重新排序的行,我正在使用标准UITableViewCell.text
属性来显示文本。当我点击编辑,移动一行,点击完成,然后点击该行时,内置UILabel
变为完全白色(文本和背景)并且不透明,并且单元格的蓝色阴影不会显示在其后面。是什么赋予了?有什么我不应该做的事情吗?我有一个hacky修复,但我想要真正的McCoy。
以下是如何重现它:
从 iPhone OS 2.2.1 SDK 中的标准“基于导航的应用程序”模板开始:
打开 RootViewController.m
取消注释
viewDidLoad
,并启用 Edit 按钮:- (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. self.navigationItem.rightBarButtonItem = self.editButtonItem; }
指定表格有几个单元格:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; }
在
tableView:cellForRowAtIndexPath:
中,添加一行来设置单元格的文本属性,因此要使用内置的 UILabel 子视图:// Set up the cell... cell.text = @"Test";
要启用重新排序,请取消注释
tableView:moveRowAtIndexPath:toIndexPath:
。默认实现是空白的,在这种情况下很好,因为模板不包含数据模型。为 Simulator、OS 2.2.1、Build and Go 配置项目。当应用程序出现时,点击编辑,然后将任意行滑动到新位置,点击完成,然后一次点击每一行。通常点击会选择一行,将其变为蓝色,并将其文本变为白色。但是在您刚刚移动的行上轻按一下,就会使 UILabel 的背景颜色为白色。结果是一个令人困惑的白色开放空间,边缘有蓝色条带。奇怪的是,在第一次虚假点击之后,另一次点击似乎可以纠正问题。
到目前为止,我已经找到了一个修复它的黑客,但我对此并不满意。它的工作原理是确保内置UILabel
是不透明的,并且在选择后立即没有背景颜色。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// hacky bugfix: when a row is reordered and then selected, the UILabel displays all crappy
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
if ([[view class] isSubclassOfClass:[UILabel class]]) {
((UILabel *) view).backgroundColor = nil;
view.opaque = NO;
}
}
// regular stuff: only flash the selection, don't leave it blue forever
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
这似乎可行,但我不希望它永远是一个好主意。解决此问题的正确方法是什么?