在表视图单元格内的 UIImageView 中设置图像
我使用 NSThread
下载一些图像。下载完所有图像后,我必须将它们放入 cell.myimageview
中。给我在用户定义的方法中设置图像的解决方案。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *bedsbaths=[NSString stringWithFormat:@"Beds:%@ Baths:%@",[[AppDeleget.statuses valueForKey:@"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:@"baths"] objectAtIndex:indexPath.row]];
cell.mlsno.text=[[AppDeleget.statuses valueForKey:@"mlsno"] objectAtIndex:indexPath.row];
cell.price.text=[[AppDeleget.statuses valueForKey:@"price"] objectAtIndex:indexPath.row];
cell.address.text=[[AppDeleget.statuses valueForKey:@"address"] objectAtIndex:indexPath.row];
cell.bedsbaths.text=bedsbaths;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
-(void)LoadImage
{
for(int x=0;x<[ListPhotos count];x++)
{
NSData *imageData =[ListPhotos objectAtIndex:x];
id path = imageData;
NSURL *url = [NSURL URLWithString:path];
NSLog(@"%@",url);
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:@selector(downloadDone:) withObject:img waitUntilDone:NO];
}
}
-(void)downloadDone:(UIImage*)img {
// I have to set the cell here. How?
cell.myimageView.image=img
}
I download some images using an NSThread
. When all the images are downloaded, I have to put them in cell.myimageview
. Give me the solution for setting an image in a user-defined method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *bedsbaths=[NSString stringWithFormat:@"Beds:%@ Baths:%@",[[AppDeleget.statuses valueForKey:@"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:@"baths"] objectAtIndex:indexPath.row]];
cell.mlsno.text=[[AppDeleget.statuses valueForKey:@"mlsno"] objectAtIndex:indexPath.row];
cell.price.text=[[AppDeleget.statuses valueForKey:@"price"] objectAtIndex:indexPath.row];
cell.address.text=[[AppDeleget.statuses valueForKey:@"address"] objectAtIndex:indexPath.row];
cell.bedsbaths.text=bedsbaths;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
-(void)LoadImage
{
for(int x=0;x<[ListPhotos count];x++)
{
NSData *imageData =[ListPhotos objectAtIndex:x];
id path = imageData;
NSURL *url = [NSURL URLWithString:path];
NSLog(@"%@",url);
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:@selector(downloadDone:) withObject:img waitUntilDone:NO];
}
}
-(void)downloadDone:(UIImage*)img {
// I have to set the cell here. How?
cell.myimageView.image=img
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 cellForRowAtIndexPath 方法本身中将图像设置为图像视图。尝试下面的代码:
You can have set image to the Image view in the cellForRowAtIndexPath method itself. Try the below code:
在您的 cellForRowAtIndexPath 中,
此方法用于在后端加载图像,因此很容易平滑滚动表格
另一种使用 ASIHTTPRequest 的方法,其中图像在后端加载,使用起来非常好,请查看 链接。它可能对你有用。
In your cellForRowAtIndexPath
this method used for loading images in backend so it's easy to smooth scrolling of table
One more method using ASIHTTPRequest in which the images are loading in backend and it's very good to use check out the link. It may be useful to you.
在包含视图控制器的
tableView:cellForRowAtIndexPath: 方法下,创建新单元格时,添加代码:
cell.imageView.image = [UIImage imageNamed@"name of image.png"];
In the containing view controller
Under the method tableView:cellForRowAtIndexPath: when creating a new cell, ad the code:
cell.imageView.image = [UIImage imageNamed@"name of image.png"];
只要保留对单元格的引用,就不会太难。我会这样处理:
As long as you keep a reference to the cell, it's not too hard. I'd go about it like this:
即使您能够在
downloadDone:
方法中设置它,稍后您也会因可重用单元而遇到问题。因此,设置图像的正确位置是tableView:cellForRowAtIndexPath:
本身。那么如何加载图像呢?将它们保存在数组或字典中。假设计数不变,我们可以使用NSMutableArray
对象来存储NSNull
单例对象的计数,然后在
tableView:cellForRowAtIndexPath:
中 存储>在
LoadImage
中的downloadDone:
Even if you are able to set it in the
downloadDone:
method, you will face a problem later because of reusable cells. So the right place for setting the image would be thetableView:cellForRowAtIndexPath:
itself. So how do you load the images? Save them in an array or a dictionary. Say the count doesn't change, we can use anNSMutableArray
object to store count number ofNSNull
singleton objects and later,in
tableView:cellForRowAtIndexPath:
in
LoadImage
in
downloadDone: