如何在uitableview中设置图像

时间:2011-05-25 09:36:08

标签: iphone

我使用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
    }

1 个答案:

答案 0 :(得分:0)

-(void)LoadImage

将img存储在NSArray中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,代码为

cell.myimageView.image=[imgArray objectAtIndex:indexPath.row];

将tableview的委托和数据源分配为

-(void)downloadDone:(UIImage*)img {
       self.tableView.delegate=self;       
       self.tableView.datasource=self;         
    }