UITableViewCell accessoryType设置,但accessoryView是零。如何设置accessoryView backgroundColor?

时间:2010-09-15 04:26:53

标签: iphone objective-c cocoa-touch ios

以下是我的细胞设置方式:

- (UITableViewCell *)tableView:(UITableView *)tableViewIn cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"subcategory";
    UITableViewCell *cell = [tableViewIn dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [[subcategories objectAtIndex:indexPath.row] title];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    cell.contentView.backgroundColor = [UIColor clearColor];

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];

    return cell;
}    

正如人们所看到的,我设置了contentView的背景,并且效果很好 - 但是当我尝试使用backgroundViewaccessoryView做同样的事情时,没有任何反应因为backgroundViewaccessoryView似乎都是零。

当设置accessoryType而不是accessoryView时,如何设置配件的背景?

3 个答案:

答案 0 :(得分:8)

来自UITableViewCell Class Reference

<强> backgroundView

  

<强>讨论

     

对于普通样式表(UITableViewStylePlain)中的单元格,默认值为nil;对于分组样式表UITableViewStyleGrouped,默认值为非nil。 UITableViewCell将背景视图添加为所有其他视图后面的子视图,并使用其当前帧位置。

<强> accessoryView的

  

<强>讨论

     

如果此属性的值不是nil,则UITableViewCell类在表视图的正常(默认)状态下使用给定视图作为附件视图;它忽略了accessoryType属性的值。提供的附件视图可以是框架提供的控件或标签或自定义视图。附件视图显示在单元格的右侧。

在您定义这些视图之前,这些视图将为零,因此您可以根据需要设置其背景。

答案 1 :(得分:1)

以下是Apple的参考资料:

@property(nonatomic, retain) UIView *accessoryView

  

如果此属性的值不是   nil,UITableViewCell类使用   附件视图的给定视图   在表视图中的正常(默认)   州;它忽略了它的价值   accessoryType属性。提供的   配件视图可以是一个   框架提供的控制或标签或   自定义视图。配件视图   出现在右侧   细胞

我在这里理解的是,如果你设置它,那么运行时将使用它并忽略accessoryType。但我怀疑反之亦然。我的意思是,如果您设置accessoryType,则accessoryView的值仍为零。

我认为这是设计目的。如果你没有设置accessoryView但是设置accessoryType,那么运行时必须决定它要做什么:如果它设置了accessoryView,那么该属性的值是不是,那么它必须忽略accessoryType,这不应该是这种情况

答案 2 :(得分:0)

我找到了解决方案here

let backgroundView = UIView()  
backgroundView.backgroundColor = tableView.backgroundColor  
cell.backgroundView = backgroundView  
相关问题