1

我有一个标准的 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;

}

谢谢,

4

2 回答 2

2

由于该contentView属性是只读的,因此我可以向其添加子视图。

[cell.contentView addSubview:containerView];

于 2012-05-31T08:13:11.373 回答
0

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

cell.backgroundView = containerView;

[cell.contentView addSubview:containerView];
于 2012-05-30T23:38:13.483 回答