代码之家  ›  专栏  ›  技术社区  ›  Prody

Cocoa Touch:如何:自定义uicolor clearcolor背景的uitableviewcell

  •  1
  • Prody  · 技术社区  · 16 年前

    我有一个 UITableView 使用自定义单元格。
    我有 MyTableViewCell : UITableViewCell 和 MyTableViewCellContentView : UIView 类。

    我所做的基本上就是 AdvancedTableViewCells demo app from Apple 稍微改变一下,在一些行上,我想使用一个清晰的背景色来显示绘制文本后面的表格背景。

    所以在 MyTableView 的 cellForRowAtIndexPath 我正在做:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"MyCell";
    
        MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        cell.someValue = indexPath.section;
        [cell finishedSetup];
    
        return cell;
    }
    

    在我的 MyTableViewCell 的 finishedSetup 以下内容:

    cellContentView = [[MyTableViewCellContentView alloc] initWithFrame:CGRectMake(0, 0, 320, 80) cell:self];
    cellContentView.autoresizingMask = UIViewAutoresizingNone;
    cellContentView.contentMode = UIViewContentModeRedraw;
    [self.contentView addSubview:cellContentView];
    

    和 在里面 MyTableViewCellContentView 我实现了 drawRect 方法。并计划不使用任何子视图,而是像Apple示例中那样绘制自定义内容。 CompositeSubviewBasedApplication .

    问题是,对于一些部分,我想使用 clearColor backgroundColor . 这是有效的,直到- 透明色 背景色 用于绘制 透明色 单元格,此时背景未被清除,仍将使用旧颜色。

    如何重新绘制背景?

    编辑 : 我忘了提,我正在设置背景色 我的表视图单元格内容视图 调用super的init后的init。通过:

    self.backgroundColor = [UIColor clearColor];
    

    我已经验证了这实际上是被调用的,并且使用clearcolor或redcolor按预期调用。

    我也试过设置表格单元格的背景色,但没用。

    编辑第2页 :这是我的DrawRect方法:

    - (void)drawRect:(CGRect)rect {
        [super drawRect:rect];
        static int i = 0;
        [[NSString stringWithFormat:@"Cell %d", ++i] drawAtPoint:CGPointMake(3, 3) withFont:[UIFont boldSystemFontOfSize:16]];
    }
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   Adam Eberbach Adil Shaikh    16 年前

    要使背景色设置生效,需要在 tableView:willDisplayCell:forRowAtIndexPath -操作系统不会改变您在这里设置的任何内容。原因是,在从返回单元后,操作系统会对单元进行一些额外的设置。 tableView:cellForRowAtIndexPath . 如果可以,请看一下由JasonBeaver提供的WWDC09中的会话101。

        2
  •  0
  •   Prody    16 年前

    我还不知道为什么会这样。但我尝试了另一种策略。我不再重用表单元,因为我没有那么多表单元。

    相反,我在启动时创建了所有的单元,并在显示它们时获得了性能的提升,因为这样滚动时就不需要额外的设置。