1

我在导航控制器中放置了一个表格视图。表格视图的单元格宽度小于原始单元格,背景是图像。如何设置“选定的颜色”?到目前为止,这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"ApplicationCell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[CompositeSubviewBasedApplicationCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                            reuseIdentifier:CellIdentifier];



    }
return cell;
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        cellContentView = [[CompositeSubviewBasedApplicationCellContentView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 0.0, 1.0) cell:self];
        cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        cellContentView.contentMode = UIViewContentModeRedraw;

        //here i'm making the cells smaller in width than the rest of the tableView
        CGRect framme = cellContentView.frame;

        framme.size.width = framme.size.width-58;
        //set the left space
        framme.origin.x = framme.origin.x+29;

        [cellContentView setFrame:framme];
        [self.contentView addSubview:cellContentView];
    }
    return self;
}

这是我未选中的表格视图

选择

4

2 回答 2

2

TableViewCells 有 abackgroundView和 a selectedBackgroundView。使用这些作为背景,只将标签和图像视图放在contentView.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        UIView* visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29.0f, 0.0f, backgroundView.bounds.size.width - 58.0f, backgroundView.bounds.size.height)];
        // configure the visibleBackgroundView with the color you want for unselected cell here
        [backgroundView addSubview:visibleBackgroundView];
        self.backgroundView = backgroundView;

        UIView* selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
        UIView* visibleSelectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29.0f, 0.0f, backgroundView.bounds.size.width - 58.0f, backgroundView.bounds.size.height)];
        // configure the visibleSelectedBackgroundView with the color you want for selection here
        [selectedBackgroundView addSubview:visibleSelectedBackgroundView];
        self.selectedBackgroundView = selectedBackgroundView;

        // configure your content view with all the labels you need here
    }
    return self;
}
于 2012-10-19T10:56:20.143 回答
1

UITableViewCell 是 UiView 的子类,因此您可以使用其上的所有 UIView 类属性,例如 backgroundColor 等。正如 Madboy 所提到的,它还具有 backgroundView 和 selectedBackgroundView 作为其属性,因此您可以使用这些属性使其成为自定义单元格。

于 2012-10-19T11:14:59.797 回答