自定义 UITableViewCell 中的不同 UIImage 大小
我正在从互联网上下载一些来自几个不同位置的图像,所有图像的尺寸未知且不同。
我已经成功地让它们全部延迟加载,并在下载到我创建的自定义 UITableViewCell 中时出现,其中有一个 UIImageView(cellImageView
宽度 320,高度 100)。
我正在设置表格的单元格并调整行高的大小,如下所示:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
ImageDownloader *download = [imagesModel.downloads objectAtIndex:[indexPath row]];
if (download.image)
{
CGFloat scaleFactor = 320.0f / download.image.size.width;
CGFloat heightScaled = (scaleFactor * download.image.size.height);
return heightScaled;
}
return 100.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ImageTableCell *cell = (ImageTableCell *)[tableView dequeueReusableCellWithIdentifier:[ImageTableCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ImageTableCell" owner:self options:nil];
cell = loadCell;
self.loadCell = nil;
}
ImageDownloader *download = [imagesModel.downloads objectAtIndex:[indexPath row]];
if (download.image == nil)
{
download.delegate = self;
}
cell.cellImageView.image = download.image;
return cell;
}
问题是我无法使单元格正确显示。我尝试过 UIImageView 的 contentMode,发现 AspectFill 工作正常,但将所有内容向上推,导致剪切和遮蔽,并在不应该的情况下拉伸一些图像。使用 Top 解决了垂直问题,但 TableViewCell 不适合图像(图像有时太大。
解决方案是调整 Table 行、自定义 TableViewCell、UIImage 和 UIImage?也许可以创建我自己的缩放功能?
I am downloading a number of images from the internet, from several different locations, all with unknown and differing dimensions.
I have successfully got them all lazy loading nicely and appearing when downloaded in the custom UITableViewCell I created, which has a UIImageView (cellImageView
width 320, height 100) inside it.
I am setting the Table's cell and resizing the row height like so:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
ImageDownloader *download = [imagesModel.downloads objectAtIndex:[indexPath row]];
if (download.image)
{
CGFloat scaleFactor = 320.0f / download.image.size.width;
CGFloat heightScaled = (scaleFactor * download.image.size.height);
return heightScaled;
}
return 100.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ImageTableCell *cell = (ImageTableCell *)[tableView dequeueReusableCellWithIdentifier:[ImageTableCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ImageTableCell" owner:self options:nil];
cell = loadCell;
self.loadCell = nil;
}
ImageDownloader *download = [imagesModel.downloads objectAtIndex:[indexPath row]];
if (download.image == nil)
{
download.delegate = self;
}
cell.cellImageView.image = download.image;
return cell;
}
Problem is I can't get the cells to display correctly. I have played around with the contentMode of the UIImageView and found the AspectFill works OK but pushes everything upwards causing clipping and masking and stretches some images when they shouldnt be. Using Top solves the vertical problem but the TableViewCell then doesn't fit the image (the image is too big sometimes.
Is the solution is to resize the Table's row, the custom TableViewCell, the UIImage and UIImage? And maybe create my own scaling functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是在IB中还是在xcode中创建了UITableViewCell?如果在 xcode 中,我有兴趣查看layoutSubviews 方法。
did you create the UITableViewCell in IB, or in xcode? if in xcode I would be interested in seeing the layoutSubviews method.
事实证明,自定义 TableViewCell、它的视图和 UIImageView 的高度都略有不同,导致结果混乱。
Turns out the custom TableViewCell, it's view and the UIImageView were all slightly different heights, giving it the messed up results.