繁体   English   中英

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

[英]Setting an image in a UIImageView inside a table view cell

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

在包含视图中 controller

在方法 tableView:cellForRowAtIndexPath: 下创建新单元格时,广告代码:

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

只要您保留对单元格的引用,就不会太难。 我想 go 是这样的:

- (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;
}

即使您能够在downloadDone:方法中设置它,但由于单元格可重用,您稍后也会遇到问题。 所以设置图像的正确位置是tableView:cellForRowAtIndexPath:本身。 那么如何加载图像呢? 将它们保存在数组或字典中。 假设计数没有改变,我们可以使用NSMutableArray object 来存储NSNull singleton 对象的计数及更高版本,

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];
}

您可以在 cellForRowAtIndexPath 方法本身中将图像设置为图像视图。 试试下面的代码:

- (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*/

    }

在你的 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 的另一种方法,其中图像在后端加载,使用检查链接非常好。 它可能对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM