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

问题描述 投票:7回答:3

以下是我的单元格设置方式:

- (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时,如何设置附件的背景?

iphone objective-c cocoa-touch ios
3个回答
8
投票

来自UITableViewCell Class Reference

backgroundView

讨论

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

accessoryView的

讨论

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

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


1
投票

这是Apple的参考:

@property(nonatomic, retain) UIView *accessoryView

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

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

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


0
投票

我找到了解决方案here

let backgroundView = UIView()  
backgroundView.backgroundColor = tableView.backgroundColor  
cell.backgroundView = backgroundView  
© www.soinside.com 2019 - 2024. All rights reserved.