gpt4 book ai didi

ios - 使用 contentView 属性,UILabel 未显示在我的表格 View 单元格中

转载 作者:行者123 更新时间:2023-11-28 22:32:16 25 4
gpt4 key购买 nike

我遇到了一个问题,UILabels 使用单元格的 contentView 属性未显示在我的表格 View 单元格中。 UILabels 在我将 UIImage 添加到单元格之前显示。我想将文本添加到单元格并使用 cell.contenView,这样我就可以将 UILAbel 放在我想要的地方。

感谢您对此的任何帮助。

这是我的代码:

- (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,单元格文本显示在右侧。

enter image description here

最佳答案

你刚刚在这里犯了一个愚蠢的错误:

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

编辑

要将您创建的标签置于 ImageView 上方,您只需添加这两行:

[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];

截图:

enter image description here

关于ios - 使用 contentView 属性,UILabel 未显示在我的表格 View 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com