将 UIImageView 添加到 Tableview

发布于 2024-12-03 15:05:35 字数 699 浏览 0 评论 0原文

我有一个异步图像加载器,它在 UIImageview 中加载图像(JImage)。我想将它们显示在表格单元格中。显然我无法设置 cell.imageView.image 因为我没有图像,我只有一个视图。

如何将 UIImageview 设置为表格单元格? cell.backgroundView 虽然可以工作,但是会覆盖整个单元格。

JImage代码是:

    NSURL *theUrl=[NSURL URLWithString:imageURL];
JImage *photoImage=[[JImage alloc] init];
[photoImage initWithImageAtURL:theUrl];
[photoImage setContentMode:UIViewContentModeRedraw];
[photoImage setFrame:CGRectMake(0.0f, 0.0f, 40.0f, 65.0f)];
cell.backgroundView = photoImage;
[photoImage release];

在JImage.m中:

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 
{
     [self setImage:[UIImage imageWithData: data]]; 
}

I have an asychronous image loader which loads images (JImage) in a UIImageview. I want to display these in a tablecell. Obviously i cant set cell.imageView.image because i dont have an image, i just have a view.

How do i set the UIImageview to the tablecell? cell.backgroundView works though, but that paints over the whole cell.

The JImage code is:

    NSURL *theUrl=[NSURL URLWithString:imageURL];
JImage *photoImage=[[JImage alloc] init];
[photoImage initWithImageAtURL:theUrl];
[photoImage setContentMode:UIViewContentModeRedraw];
[photoImage setFrame:CGRectMake(0.0f, 0.0f, 40.0f, 65.0f)];
cell.backgroundView = photoImage;
[photoImage release];

which in the JImage.m:

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 
{
     [self setImage:[UIImage imageWithData: data]]; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狂之美人 2024-12-10 15:05:35

使用此代码并根据您的需要设置框架。

UIImage *imagearrow = [UIImage imageNamed:@"arrow.png"];    
UIImageView *imgarrow=[[UIImageView alloc] initWithFrame:CGRectMake(290, 25, 7, 15)];
imgarrow.image =imagearrow;
[cell addSubview:imgarrow];
[imagearrow release];

use this code and set frame according to your need.

UIImage *imagearrow = [UIImage imageNamed:@"arrow.png"];    
UIImageView *imgarrow=[[UIImageView alloc] initWithFrame:CGRectMake(290, 25, 7, 15)];
imgarrow.image =imagearrow;
[cell addSubview:imgarrow];
[imagearrow release];
桃气十足 2024-12-10 15:05:35

如果您只想异步加载图像,则不需要使用 JImage。一种简单的方法是使用 GCD 和块,如下所示: https://github.com/ChrisTec/iPhone-Book-CodeSamples/blob/master/Chapter%2013/RealEstateViewer%2013.2.5/RealEstateViewer/ImageTableViewController.m

这是示例章节我与人合着的《Objective-C Fundamentals》一书深入解释了这段代码:http://www.manning.com/fairbairn/OCF_sample_ch13.pdf

总结一下:请求单元格,您查看是否已经下载了该图像。如果有,则显示它。如果没有,您将显示一个微调器并启动一个异步 GCD 块来获取图像。该块完成后,它将在主线程上运行另一个块,以切换图像的旋转器。就这样。

You don't need to use JImage if all you want to do is load the image asynchronously. One easy way to do this is with GCD and blocks, like so: https://github.com/ChrisTec/iPhone-Book-CodeSamples/blob/master/Chapter%2013/RealEstateViewer%2013.2.5/RealEstateViewer/ImageTableViewController.m

Here's a sample chapter of the book Objective-C Fundamentals which I've co-authored that explains this code in depth: http://www.manning.com/fairbairn/OCF_sample_ch13.pdf

To sum it up: When the cell is requested, you look if you already have downloaded the image. If you have, you display it. If not, you display a spinner and start an async GCD block that will fetch the image. One that block is done, it will run another block on the main thread that switches the spinner out for the image. That's all.

诗笺 2024-12-10 15:05:35

我认为你应该采取不同的方法来解决这个问题。不必将 JImage 放入单元格中,而是将其保存在视图控制器中的其他位置,NSArray 通常很方便。然后用占位符图像填充单元格或将其留空。

因此,假设您有一个包含正在加载的所有 JImage 的数组,当数组位置 x 中的 JImage 完成加载时,您将重新加载该特定单元格,如下所示:

NSIndexPath indexPath = [NSIndexPath indexPathForRow:x inSection:0];
[self.tableView reloadRowsAtIndexPaths:x withRowAnimation:NO];

在您的 cellForRow:AtIndexPath您只需从数组中保存的 JImage 对象中获取图像即可。

我希望这对你有帮助

I think you should take a different approach to this. Instead of putting your JImage in the cell, hold it somewhere else in the view controller, an NSArray is usually convenient. Then populate the cells with either placeholder images or just leave them empty.

So let's say you have an array with all the JImages that are loading, when the JImage in the position x of the array finishes loading, you reload that specific cell as it follows:

NSIndexPath indexPath = [NSIndexPath indexPathForRow:x inSection:0];
[self.tableView reloadRowsAtIndexPaths:x withRowAnimation:NO];

And in your cellForRow:AtIndexPath you just have to take the image from the JImage object held in the array.

I hope this helps you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文