fizampou
fizampou

Reputation: 717

UITableViewCell width

I have a table view placed in a navigation controller. Cells of the table view are smaller in width than the original and the background is an image. How can i set the "selected color"? here is my code so far:

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

here is my table view unselected

selected

Upvotes: 1

Views: 7594

Answers (2)

Michael Ochs
Michael Ochs

Reputation: 2870

TableViewCells have a backgroundView and a selectedBackgroundView. Use these for the background and only put the labels and image views in the 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;
}

Upvotes: 2

kidsid49
kidsid49

Reputation: 1378

UITableViewCell is a subclass of UiView so you can use all the UIView class properties on it like backgroundColor etc. As Madboy mentioned its also has backgroundView and selectedBackgroundView also as its properties so you can play with theses properties to make it a customised cell.

Upvotes: 1

Related Questions