我想在UITableViewCell中添加文本标签或UIImage(无所谓)。 但它没有出现。我在这个单元格中使用了很少的视图。你可以看到它在下面的代码:iOS UITableViewCell添加标签和图像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"timelineCell";
KMWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell)
{
cell = [cell initWithFrame:CGRectMake(13, 10, 293, 200)];
UIView *cellContentView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, 293, 200)];
cellContentView.layer.cornerRadius = 5;
cellContentView.layer.shadowOffset = CGSizeMake(1, 0);
cellContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
cellContentView.layer.shadowRadius = 5;
cellContentView.layer.shadowOpacity = .25;
cellContentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBack"]];
UIView *selectedContentView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, 293, 200)];
selectedContentView.layer.cornerRadius = 5;
selectedContentView.layer.shadowOffset = CGSizeMake(1, 0);
selectedContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
selectedContentView.layer.shadowRadius = 5;
selectedContentView.layer.shadowOpacity = .25;
selectedContentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBackActive"]];
UIView *visibleContentView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, 293, 200)];
visibleContentView.layer.cornerRadius = 5;
visibleContentView.layer.shadowOffset = CGSizeMake(1, 0);
visibleContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
visibleContentView.layer.shadowRadius = 5;
visibleContentView.layer.shadowOpacity = .25;
visibleContentView.backgroundColor = [UIColor clearColor];
//Image adding here:
[ UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"active.png"]];
[visibleContentView addSubview:image];
[cellContentView addSubview:visibleContentView];
[selectedContentView addSubview:visibleContentView];
[cell setBackgroundView:cellContentView];
[cell setSelectedBackgroundView:selectedContentView];
}
return cell;
}
正如你可以看到我使用backgroundView和selectedBackgroundView,并查看与其他clearColor信息。那么为什么其他元素不能在visibleView中显示呢?
UPDATE:
我使用的子类,因为我需要改变细胞的帧大小。
如果您使用的是“UITableViewCell”子类,您为什么还在做这一切? – Desdenova
这不是关于问题,但为什么要将'visibleContentView'添加到'cellContentView'和'selectedContentView'两个视图中。你为什么不简单地将它添加到'selectedContentView'中。 –
我认为这是问题! –