首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >已设置UITableViewCell accessoryType,但accessoryView为nil。如何设置accessoryView backgroundColor?

已设置UITableViewCell accessoryType,但accessoryView为nil。如何设置accessoryView backgroundColor?
EN

Stack Overflow用户
提问于 2010-09-15 12:26:53
回答 3查看 11.4K关注 0票数 7

下面是我的单元格的设置:

代码语言:javascript
复制
- (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时,如何设置附件的背景?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-15 12:43:54

来自UITableViewCell Class Reference

backgroundView

讨论

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

accessoryView

讨论

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

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

票数 8
EN

Stack Overflow用户

发布于 2010-09-15 12:40:06

下面是来自苹果的推荐信:

@property(nonatomic, retain) UIView *accessoryView

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

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

我想这就是设计的目的。如果您不设置accessoryView而是设置accessoryType,那么运行时必须决定它必须做什么:如果它设置了accessoryView,则该属性的值不是nil,然后它必须忽略accessoryType,这不应该是这种情况

票数 1
EN

Stack Overflow用户

发布于 2017-12-20 03:15:58

我找到了一个解决方案here

代码语言:javascript
复制
let backgroundView = UIView()  
backgroundView.backgroundColor = tableView.backgroundColor  
cell.backgroundView = backgroundView  
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3714676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档