UITableViewCell accessoryType设置,但accessoryView为零。 我如何设置accessoryView backgroundColor?
以下是我的细胞设置:
- (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
,如何去设置附件的背景?
来自UITableViewCell类参考 :
backgroundView
讨论
对于普通样式表(UITableViewStylePlain)中的单元格,默认值为零,对于分组样式表UITableViewStyleGrouped,默认值为零。 UITableViewCell将背景视图添加为所有其他视图背后的子视图,并使用其当前帧位置。
accessoryView的
讨论
如果这个属性的值不为零,那么UITableViewCell类在表视图的正常(默认)状态下使用给定的视图作为附件视图; 它忽略了accessoryType属性的值。 提供的附件视图可以是框架提供的控件或标签或自定义视图。 附件视图出现在单元格的右侧。
直到你定义它们,这些视图将是零,所以你可以根据需要设置它们的背景。
以下是苹果的参考资料:
@property(nonatomic, retain) UIView *accessoryView
如果这个属性的值不为零,那么UITableViewCell类在表视图的正常(默认)状态下使用给定的视图作为附件视图; 它忽略了accessoryType属性的值。 提供的附件视图可以是框架提供的控件或标签或自定义视图。 附件视图出现在单元格的右侧。
我在这里理解的是,如果你设置它,那么运行时将使用它并忽略accessoryType
。 但我怀疑反之亦然。 我的意思是,如果你设置了accessoryType
,那么accessoryType
的值将仍然是零。
我认为这是devise目的。 如果你没有设置accessoryView
而是设置了accessoryType
,那么运行时必须决定它要做什么:如果它设置了accessoryView
,那么该属性的值不是零,那么它必须忽略accessoryType
不应该是这样的
我在这里find了解决办法
let backgroundView = UIView() backgroundView.backgroundColor = tableView.backgroundColor cell.backgroundView = backgroundView