objective-c - 更改 UITableViewCell 的选定状态

标签 objective-c ios uitableview

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

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

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

}

谢谢,

最佳答案

因为 contentView 属性是只读的,所以我可以向它添加一个 subview 。

[cell.contentView addSubview:containerView];

关于objective-c - 更改 UITableViewCell 的选定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10823042/

相关文章:

ios - 如何根据条件在核心数据中插入数据

objective-c - 通过 NSMutableArray 的引用或值 - (void)addObjectsFromArray?

objective-c - SSH 键盘交互认证

php - 在 mysql php webservice 中搜索

ios - 在图表的 PieChartView 中格式化自定义图例条目?

ios - Swift 使用 'as' 运算符将 Bool 转换为 NSNumber

ios - 将信息从 TableView 传递到 View Controller 不是基于 indexPathForSelectedRow,而是基于所选单元格的标签文本?

iphone - UITableViewCell 滚动时复选框重复

ios - 分组 TableView - 了解选择了哪个单元格

ios - 比较值并在 uitableview 中显示特定结果