1

各セルがカスタム UIView である標準の UITableView があります。セルが選択されている場合を除いて、すべてが正常に機能し、(予想どおり) 青に変わりますが、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 に答える