[英]UILabels are not displaying in my table view cells using contentView property
我遇到的問題是UILabels
沒有使用單元格的contentView屬性顯示在我的表視圖單元格中。 在我向細胞添加UIImage之前, UILabels
正在展示。 我想將文本添加到單元格並使用cell.contenView,這樣我就可以將UILAbels放在我想要的位置。
感謝您對此的任何幫助。
這是我的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = @"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.tag = 234234;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:@"DIN" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
titleLabel.layer.shouldRasterize=YES;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 345345;
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:@"DIN" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
detailLabel.layer.shouldRasterize=YES;
[cell.contentView addSubview:detailLabel];
}
UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"];
NSString *imageUrlString = [NSString stringWithFormat:@"%@", [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"]];
UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString];
if (cachedImage)
{
cell.imageView.image = cachedImage;
}
else
{
cell.imageView.image = [UIImage imageNamed:@"rest.jpg"]; // initialize the image with blank image as a placeholder
// download in the image in the background
[self.imageDownloadingQueue addOperationWithBlock:^{
NSURL *imageUrl = [NSURL URLWithString:imageUrlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = nil;
if (imageData)
image = [UIImage imageWithData:imageData];
if (image)
{
[self.imageCache setObject:image forKey:imageUrlString]; // add the image to your cache
// finally, update the user interface in the main queue
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible
UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
cell.imageView.image = image;
}];
}
}];
}
return cell;
}
另外,當我添加cell.textLabel.text = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
而不是使用contentView,單元格文本顯示在右側。
你在這里犯了一個愚蠢的錯誤:
detailLabel.textColor = [UIColor whiteColor];
你試圖在白色背景上顯示白色字體顏色?
我剛剛測試了你的代碼如下,它顯示正確:
UILabel *titleLabel;
UILabel *detailLabel;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = @"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.tag = 234234;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor blackColor];
[titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
titleLabel.layer.shouldRasterize=YES;
[cell.contentView addSubview:titleLabel];
detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 345345;
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor blackColor];
[detailLabel setFont:[UIFont boldSystemFontOfSize:15]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
detailLabel.layer.shouldRasterize=YES;
[cell.contentView addSubview:detailLabel];
[cell.contentView bringSubviewToFront:titleLabel];
[cell.contentView bringSubviewToFront:detailLabel];
}
detailLabel = (UILabel *)[cell viewWithTag:345345];
detailLabel.text = [_objects objectAtIndex:indexPath.row];
titleLabel = (UILabel *)[cell viewWithTag:234234];
titleLabel.text = [_objects objectAtIndex:indexPath.row];
return cell;
}
編輯
要將創建的標簽帶到圖像視圖上方,您只需添加以下兩行:
[cell.contentView bringSubviewToFront:titleLabel];
[cell.contentView bringSubviewToFront:detailLabel];
另外,添加圖像contentView如下所示:
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 40 ,40)];
imgView.image = [UIImage imageNamed:@"img.png"];
[cell.contentView addSubview:imgvie];
截圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.