我想在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'中。 –
我認爲這是問題! –