在表视图单元格内的UIImageView中设置图像

时间:2011-05-25 07:03:19

标签: objective-c cocoa-touch ios uitableview 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
}

5 个答案:

答案 0 :(得分:2)

在包含视图控制器中

在方法tableView:cellForRowAtIndexPath下:创建新单元格时,请输入代码:

cell.imageView.image = [UIImage imageNamed@"name of image.png"];

答案 1 :(得分:0)

只要您保留对细胞的参考,就不会太难。我会这样做:

- (void)downloadDone:(UIImage*)img {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:yourCellRow inSection:yourCellSection];
    UITableViewCell *cell = [myTableView cellForRowAtIndexPath:indexPath];
    // now you have the cell you wish to modify;
    cell.myimageView.image=img;
    [myTableView reloadData];
    // if you would prefer, you could also call [myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]
    // if you only want to reload that specific cell;
}

答案 2 :(得分:0)

即使您能够使用downloadDone:方法进行设置,由于可重复使用的单元格,您将在以后遇到问题。因此,设置图像的正确位置是tableView:cellForRowAtIndexPath:本身。那么你如何加载图像?将它们保存在数组或字典中。假设计数没有改变,我们可以使用NSMutableArray对象来存储NSNull单例对象的计数数量,之后,

tableView:cellForRowAtIndexPath:

中的

if ( [[images objectAtIndex:indexPath.row] isMemberOfClass:[UIImage class]] ) {
    cell.myImageView.image = [images objectAtIndex:indexPath.row];
}
LoadImage

中的

for(int x=0;x<[ListPhotos count];x++)
{   
    ... 
    [photos replaceObjectAtIndex:x withObject:image];
    [self performSelectorOnMainThread:@selector(downloadDone:) 
                           withObject:[NSNumber numberWithInt:x];
                        waitUntilDone:NO];
}
downloadDone:

中的

- (void)downloadDone:(NSNumber *)row {

    [self.tableView reloadRowsAtIndexPaths:[NSIndexPath indexPathForRow:[row intValue] inSection:0]
                          withRowAnimation:UITableViewRowAnimationTop];
}

答案 3 :(得分:0)

您可以将图像设置为cellForRowAtIndexPath方法本身的Image视图。请尝试以下代码:

- (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;
    NSString *filePath = [self getImagePath];
    UIImage * img = [UIImage imageWithContentsOfFile:filePath];
    cell.myimageView.image=img;
    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    return cell;

}
    -(NSString *)getImagePath
    {
      /*here you can get the path of your image here*/

    }

答案 4 :(得分:0)

在你的cellForRowAtIndexPath

UIImageView *logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 55)];
logoImgView.backgroundColor = [UIColor clearColor];
 [cell.Imageview addSubview:logoImgView];
 NSMutableArray *arrChng = [[NSMutableArray alloc] init];
 [arrChng addObject:logoImgView];
 [arrChng addObject:[Array objectAtIndex:indexPath.row];
 [self performSelectorInBackground:@selector(setImagesAfterShow:) withObject:arrChng];


-(void)setImagesAfterShow:(NSMutableArray *)array
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:[array objectAtIndex:1]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImageView *img = [array objectAtIndex:0];
img.image = [UIImage imageWithData:data];
[pool release];
}

此方法用于在后端加载图像,因此可以轻松平滑滚动表格

另一种使用ASIHTTPRequest的方法,其中图像在后端加载,并且非常好用,请查看link。它可能对你有用。