本文介绍了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

解决方案

从:



backgroundView


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


这些视图将是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

accessoryView

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:03