下面是我的单元格的设置:
- (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的背景,这很好用-但当我尝试用backgroundView或accessoryView做同样的事情时,什么也没有发生,因为backgroundView和accessoryView似乎都是空的。
当设置accessoryType而不是accessoryView时,如何设置附件的背景?
发布于 2010-09-15 12:43:54
来自UITableViewCell Class Reference
backgroundView
讨论
对于纯样式表格(UITableViewStylePlain)中的单元格,默认值为nil;对于分组样式表格( UITableViewStyleGrouped),默认值为非nil。UITableViewCell将背景视图添加为所有其他视图后面的子视图,并使用其当前框架位置。
accessoryView
讨论
如果此属性的值不为accessoryType,则UITableViewCell类在表视图的正常(默认)状态下使用附件视图的给定视图;它会忽略nil属性的值。提供的附件视图可以是框架提供的控件或标签,也可以是自定义视图。附件视图出现在单元格的右侧。
在您定义这些视图之前,这些视图将为空,因此您可以根据需要设置它们的背景。
发布于 2010-09-15 12:40:06
下面是来自苹果的推荐信:
@property(nonatomic, retain) UIView *accessoryView
如果此属性的值不为nil,则UITableViewCell类在表视图的正常(默认)状态下使用附件视图的给定视图;它将忽略accessoryType属性的值。提供的附件视图可以是框架提供的控件或标签,也可以是自定义视图。附件视图出现在单元格的右侧。
我在这里所理解的是,如果您设置了它,那么运行时将使用它并忽略accessoryType。但我怀疑反之亦然。我的意思是,如果您设置了accessoryType,那么accessoryView的值仍然是零。
我想这就是设计的目的。如果您不设置accessoryView而是设置accessoryType,那么运行时必须决定它必须做什么:如果它设置了accessoryView,则该属性的值不是nil,然后它必须忽略accessoryType,这不应该是这种情况
发布于 2017-12-20 03:15:58
我找到了一个解决方案here
let backgroundView = UIView()
backgroundView.backgroundColor = tableView.backgroundColor
cell.backgroundView = backgroundView https://stackoverflow.com/questions/3714676
复制相似问题