更改UITableViewCell的选定状态

时间:2012-05-30 19:34:30

标签: objective-c ios uitableview

我有一个标准的UITableView,其中每个单元格都是自定义的UIView。一切正常,除非选择单元格,它变成蓝色(如预期的那样),但UIView是隐藏的!

如何在选择单元格时将视图内容更改为可见?

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

}

谢谢,

2 个答案:

答案 0 :(得分:2)

由于contentView属性是readonly,我可以添加子视图。

[cell.contentView addSubview:containerView];

答案 1 :(得分:0)

您正在向背景添加视图,而不是单元格的contentView替换此行。

cell.backgroundView = containerView;

[cell.contentView addSubview:containerView];