我正在尝试在 UITableViewCell 中添加文本标签或 UIImage(无关紧要)。 但它没有出现。我在这个单元格中使用了很少的 View 。您可以在下面的代码中看到它:
- (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中呢?
更新:
我正在使用子类,因为我需要更改单元格的帧大小。
最佳答案
我认为问题在于,您在将 visibleContentView
添加到 cellContentView
后立即将其添加到 selectedContentView
中。这会将其从 cellContentView
中删除,因为一个 View 一次只能有一个 super View 。所以 visibleContentView
实际上位于 selectedContentView
中。如果 KMWTableViewCell
工作正常,则在选择单元格时您应该会看到缺少的组件。
关于iOS UITableViewCell 添加标签和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17747359/