2009-02-19 28 views
4

我有一个UITableView与可重新配置的行,我使用标准UITableViewCell.text属性来显示文本。当我点击编辑,移动一行,点击完成,然后点击该行,内置的UILabel变为完全白色(文本和背景)和不透明,单元格的蓝色阴影不显示在它后面。是什么赋予了?有没有我应该做的事情,我不是?我有一个哈克修理,但我想要真正的麦考伊。移动UITableView行后,选择它,它只在边缘变为蓝色

下面是如何重现它:

与在iPhone OS 2.2.1 SDK标准 “为基础的导航应用程序” 模板开始:

  1. 打开RootViewController.m

  2. 取消注释viewDidLoad,并启用编辑按钮:

    - (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. 配置模拟器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]; 
} 

这似乎工作,但我不认为它永远是一个好主意。什么是解决这个问题的正确方法?

回答

6

这看起来像是UITableView渲染中的一个错误,您应该为其提供一个雷达错误报告。这就像移动后细胞没有得到适当的刷新。解决这个现在

一种方法是不使用内置的标签,但推出自己的细胞:

- (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; 
} 

我想这一点,并没有表现出相同的排序你用内置标签看到的白色空白矩形。但是,向表格单元添加另一个不透明视图可能不是最佳的整体渲染性能。

我不知道这是一个小故障,因为苹果公司不希望你坚持在表格行选择突出显示(他们一直在审查过程中最近实施)。您应该选择一个复选标记或移动到导航层次结构中的下一个级别,此时此白色框仅在屏幕上显示几分之一秒。

+0

+1在选择笔记上。选择并不意味着长时间显示! (我们在几款应用中遇到了这个问题) – 2009-03-01 03:59:26

+0

I concur;我们只是rev'd填字游戏,以消除使用突出显示的行来指示持久选择 – 2009-03-01 19:47:56

2

在从布拉德溶液中的特技似乎是:

textLabel.backgroundColor = [的UIColor clearColor];

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

也许一个完全自定义的绘制单元格会以某种方式修复它。尽管如此,我还没有尝试过。

其他人是否有解决方案?

1

感谢您的信息,我正在搜索如何从UILabel中清除背景颜色。

我用下面的行:

textLabel.backgroundColor = [的UIColor clearColor];

和完美的工作!

感谢

亚历杭:)

-1

选择是不是要显示长时间! (我们碰到了几个应用程序)

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