UITableViewCell attachmentType set,但是attachmentView为nil。如何设置accessoryView backgroundColor? [英] UITableViewCell accessoryType set, but accessoryView is nil. How do I set the accessoryView backgroundColor?

查看:96
本文介绍了UITableViewCell attachmentType set,但是attachmentView为nil。如何设置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 code>,没有什么发生,因为 backgroundView accessoryView 都似乎为零。



当设置 accessoryType 而不是 accessoryView

解决方案

UITableViewCell类参考



backgroundView


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



讨论



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


这些视图将是nil,直到您定义它们,因此您可以根据需要设置其背景。


Here's how my cells are set up:

- (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;
}    

As one can see, I set the background of the contentView, and that works just fine - but when I try to do the same thing with backgroundView or accessoryView, nothing happens because backgroundView and accessoryView both seem to be nil.

When one sets the accessoryType rather than the accessoryView, how does one then go about setting the background of the accessory?

解决方案

From UITableViewCell Class Reference:

backgroundView

Discussion

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for grouped-style tables UITableViewStyleGrouped). UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.

accessoryView

Discussion

If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s normal (default) state; it ignores the value of the accessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the the right side of the cell.

These views will be nil until you define them, and so you can set their backgrounds as desired.

这篇关于UITableViewCell attachmentType set,但是attachmentView为nil。如何设置accessoryView backgroundColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆