Max Woolf
Max Woolf

Reputation: 4058

Altering selected state for UITableViewCell

I've got a standard UITableView where each cell is a custom UIView. Everything works fine, except when the cell is selected, it turns blue (as expected) but the UIView is hidden!

How can I change the contents of the view to be visible when the cell is selected?

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView 
                         dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
}
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 40)];
[headerLabel setText:[[[todayCollection events]objectAtIndex:indexPath.row]name]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:22]];
headerLabel.highlightedTextColor = [UIColor whiteColor];

UILabel *venueLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 300, 20)];
[venueLabel setText:[[[[todayCollection events]objectAtIndex:indexPath.row]venue]name]];
[venueLabel setFont:[UIFont italicSystemFontOfSize:16]];

LBVenueBadge *venueBadge = [[LBVenueBadge alloc] initWithGenre:[[[todayCollection events]objectAtIndex:indexPath.row]genre] frame:CGRectMake(85, 73, 100, 20)];
UIImageView *pinImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 75, 18, 18)];
[pinImage setImage:[UIImage imageNamed:@"193-location-arrow.png"]];
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 75, 100, 15)];
[distanceLabel setFont:[UIFont systemFontOfSize:12]];
NSString *distanceString = [[[[todayCollection events]objectAtIndex:indexPath.row]venue]distanceString]; 

if([[[[todayCollection events]objectAtIndex:indexPath.row]performances]count] == 1)
{
    UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
    [performanceLabel setText:[[[[[todayCollection events]objectAtIndex:indexPath.row]performances]objectAtIndex:0]time]];
    [performanceLabel setBackgroundColor:[UIColor clearColor]];
    [performanceLabel setFont:[UIFont systemFontOfSize:12]];

    [containerView addSubview:performanceLabel];
}else{
    UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
    [performanceLabel setText:[NSString stringWithFormat:@"%i shows", [[[[todayCollection events]objectAtIndex:indexPath.row]performances]count]]];
    [performanceLabel setBackgroundColor:[UIColor clearColor]];
    [performanceLabel setFont:[UIFont systemFontOfSize:12]];

    [containerView addSubview:performanceLabel];
}

[distanceLabel setText:distanceString];  
[containerView addSubview:pinImage];

[distanceLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:distanceLabel];

[headerLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:headerLabel];
[venueLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:venueLabel];

[containerView addSubview:venueBadge];
if(indexPath.row % 2 == 0)
{
    [containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.05]];
}else{
    [containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.02]];
}
cell.backgroundView = containerView;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

}

Thanks,

Upvotes: 1

Views: 244

Answers (2)

railwayparade
railwayparade

Reputation: 5156

You are adding views to the background rather than the contentView of the cell replace this line.

cell.backgroundView = containerView;

with

[cell.contentView addSubview:containerView];

Upvotes: 0

Max Woolf
Max Woolf

Reputation: 4058

As the contentView property is readonly, I can add a subview to it instead.

[cell.contentView addSubview:containerView];

Upvotes: 2

Related Questions