4

我有一个UITableView可重新排序的行,我正在使用标准UITableViewCell.text属性来显示文本。当我点击编辑,移动一行,点击完成,然后点击该行时,内置UILabel变为完全白色(文本和背景)并且不透明,并且单元格的蓝色阴影不会显示在其后面。是什么赋予了?有什么我不应该做的事情吗?我有一个hacky修复,但我想要真正的McCoy。

以下是如何重现它:

从 iPhone OS 2.2.1 SDK 中的标准“基于导航的应用程序”模板开始:

  1. 打开 RootViewController.m

  2. 取消注释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;
    }
    
  3. 指定表格有几个单元格:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 4;
    }
    
  4. tableView:cellForRowAtIndexPath:中,添加一行来设置单元格的文本属性,因此要使用内置的 UILabel 子视图:

    // Set up the cell...
    cell.text = @"Test";
    
  5. 要启用重新排序,请取消注释tableView:moveRowAtIndexPath:toIndexPath:。默认实现是空白的,在这种情况下很好,因为模板不包含数据模型。

  6. 为 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];
}

这似乎可行,但我不希望它永远是一个好主意。解决此问题的正确方法是什么?

4

4 回答 4

6

这看起来像是 UITableView 渲染中的一个错误,您应该提交一份 Radar 错误报告。就像移动后单元格没有正确刷新一样。

目前解决此问题的一种方法是不使用内置标签,而是在单元格中滚动您自己的标签:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        CGRect frame = cell.contentView.bounds;
        frame.origin.x = frame.origin.x + 10.0f;

        UILabel *textLabel = [[UILabel alloc] initWithFrame:frame];
        [textLabel setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
        textLabel.tag = 1;
        textLabel.textAlignment = UITextAlignmentLeft;
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = [UIColor blackColor];
        textLabel.font = [UIFont boldSystemFontOfSize:20.0];
        textLabel.numberOfLines = 1;
        textLabel.highlightedTextColor = [UIColor whiteColor];
        [cell.contentView addSubview:textLabel];
        [textLabel release];

    }

    UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
    textLabel.text = @"Test";

    return cell;
}

我试过这个,它没有显示出与内置标签相同的白色空白矩形。但是,将另一个非透明视图添加到表格单元格可能不是整体渲染性能的最佳选择。

我不知道这是一个多么严重的故障,因为 Apple 不希望您在表格行上保留选择突出显示(他们最近在审查过程中一直在执行此操作)。您应该选中复选标记或通过选择移动到导航层次结构中的下一个级别,此时此白色框只会在屏幕上显示几分之一秒。

于 2009-02-26T01:27:30.657 回答
2

布拉德解决方案的诀窍似乎是:

textLabel.backgroundColor = [UIColor clearColor];

如果您将背景保留为默认值,即使您滚动自己的单元格 UITableViewCells,您仍然会遇到问题。我将其保留为默认值的原因是因为文档说使用不透明背景的计算成本更低。理想情况下,我不想使用 [UIColor clearColor] 来修复此错误。

也许一个完全定制的涂漆单元会以某种方式修复它。我以前没有尝试过这些。

还有其他人对此有解决方案吗?

于 2009-04-06T07:54:41.253 回答
1

感谢您提供信息,我正在寻找如何从 UILabel 中擦除背景颜色。

我使用了以下行:

textLabel.backgroundColor = [UIColor clearColor];

并且工作完美!!!

谢谢

亚历杭德拉:)

于 2009-06-16T22:44:52.370 回答
-1

选择并不意味着长时间显示!(我们的几个应用程序都遇到了这个问题)

???这意味着 Apple 不会在 iPhone 上批准他们自己的日历应用程序!当您去编辑事件的开始和结束时间时,开始时间是无限期选择的,只有在用户点击下一个字段时才会更改。

于 2010-02-27T00:42:38.190 回答