gpt4 book ai didi

objective-c - xcode ios : in cell image overlaps on the label

转载 作者:行者123 更新时间:2023-11-29 13:24:09 25 4
gpt4 key购买 nike

我正在表格中的单元格上显示图像。我有代码在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

虽然我正在使用 Storyboard,所以我在单元格中显示了图像

cell.imageView.image = [UIImage imageNamed:_lotImagesArray[row]];

但是,当我尝试从网络服务器加载图像时,图像位于单元格中标签的顶部(阻挡标签文本)

我用来显示来自网络服务器的图像的代码是这样的:

NSString *strURL = [NSString stringWithFormat:@"http://www.domainhere.com/images/%@", lotPhoto[row]];
NSURL *url = [[NSURL alloc] initWithString:strURL ];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

有人可以告诉我哪里出错了吗?

最佳答案

几个可能的可能性:

首先,您可能需要仔细检查以确保您拥有

cell.imageView.clipsToBounds = YES;

如果不这样做,当它调整图像大小以适应 UIImageView 时,图像可能会溢出 ImageView 的边界。我注意到这个问题,尤其是当我在后台队列中加载此图像时。

其次,如果您在后台设置 UITableViewCellimageView 属性(如下面的简化示例代码),您应该知道在开始背景图像加载过程之前用空白图像正确初始化图像很重要。因此,从基于 Web 的源加载单元格时,非常常见的代码示例如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.textLabel.text = ... // you configure your cell however you want

// make sure you do this next line to configure the image view

cell.imageView.image = [UIImage imageNamed:@"blankthumbnail.png"];

// now let's go to the web to get the image

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

UIImage *image = ... // do the time consuming process to download the image

// if we successfully got an image, remember, ALWAYS update the UI in the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// let's make sure the cell is still visible (i.e. hasn't scrolled off the screen)
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell)
{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
}
});
});

return cell;
}

显然,如果您执行类似的操作,您需要确保 [UIImage imageNamed:@"blankthumbnail.png"]; 没有返回 nil(即确保您的应用在 bundle 中成功找到它)。常见问题可能包括完全没有空白图像、名称错误、未能将图像包含在“构建阶段”选项卡下的“复制捆绑资源”下的 Target 设置中。

第三,您需要确保在使用子类 UITableViewCell 条目时,您没有使用标准的 UITableViewCell imageViewtextLabel 等的属性名称。确保使用您自己的唯一名称。如果您使用 imageView,系统会混淆您的新 IBOutletUITableViewCell 的默认 imageView 属性。

关于objective-c - xcode ios : in cell image overlaps on the label,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13594830/

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