UITableViewCell图像加载

时间:2014-09-15 10:58:34

标签: ios objective-c xcode uitableview

我有以下代码从Web服务加载图像,我想检查图像是否已加载,然后填充带有图像的按钮(按钮已经就位)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier forIndexPath:indexPath];

    if (!cell) {
        cell = [[FeedTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewCellIdentifier];
    }

    // Configure the cell...
    cell.post_time.font = [UIFont fontWithName:@"ProximaNova-Bold" size:12];
    cell.post_price.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
    cell.post_address.font = [UIFont fontWithName:@"ProximaNova-Regular" size:13];
    cell.post_attributes.font = [UIFont fontWithName:@"ProximaNova-Regular" size:11];
    House * house = [Posts objectAtIndex:indexPath.row];

    //cell.post_image.clipsToBounds = YES;
    //cell.post_image.contentMode = UIViewContentModeScaleAspectFill;
    //cell.post_image.contentMode = UIViewContentModeScaleAspectFit;
    cell.post_time.text = house.house_ts;
//  [cell.post_image setImageWithURL:[NSURL URLWithString:house.house_aws_image_id]];
    [cell.post_image setImageWithURL:[NSURL URLWithString:house.house_aws_image_id] placeholderImage:[UIImage imageNamed:@"splash_image_gray"]];


   cell.post_attributes.text = [self decodeAttributes:house.house_bed_rms :house.house_bath_rms :house.house_house_siz];


    [cell.post_playButton addTarget:self action:@selector(showVideo:) forControlEvents:UIControlEventTouchUpInside];
    cell.post_share.tag = indexPath.row;
    [cell.post_share addTarget:self action:@selector(shareoptions:) forControlEvents:UIControlEventTouchUpInside];

    //[cell.post_playButton setImage:[UIImage imageNamed:@"feed_play_but_pink.png"] forState:UIControlStateHighlighted];
    //[cell.post_playButton setBackgroundImage:[UIImage imageNamed:@"heart_btn"] forState:UIControlStateNormal];

    [cell.post_playButton setTag:indexPath.row];
    [cell.post_playButton setBackgroundImage:[UIImage imageNamed:@"feed_play_but"] forState:UIControlStateNormal];

    return cell;
}

任何想法如何实现这一目标?提前谢谢。

1 个答案:

答案 0 :(得分:0)

假设您将数据从Web服务作为数组获取,您应该解析数组并将其复制为可变(或直接将其解析为可变数组)。

然后你可以这样做:

id imageObject = [[[self dataArray] objectAtIndex:[indexPath row]] objectForKey:@"imageURL"];

if ([imageObject isKindOfClass:[NSString class]]) {
    NSURLConnection *connection = [NSURLConnection ...];

    // replace image url in array so that you don't start connections too often.
    [[[self dataArray] objectAtIndex:[indexPath class]] setObject:connection forKey:@"imageURL"];

    // when connection finishes, replace the connection by the instance of UIImage and reload the corresponding cell
}
else if ([imageObject isKindOfClass:[UIImage class]]) {
    [[cell imageView] setImage:imageObject];
}

您可以使用JWURLConnection class进行简单的块编程。如果您这样做,您可以轻松使用此代码(来自我的某个应用程序并且工作正常):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSMutableDictionary *post = [[self dataSource] objectAtIndex:[indexPath row]];

    id memberCloseup = [post objectForKey:@"image"];

    if ([memberCloseup isKindOfClass:[NSString class]]) {
        JWURLConnection *connection = [JWURLConnection connectionWithGETRequestToURL:[NSURL URLWithString:memberCloseup] delegate:nil startImmediately:NO];
        [connection setFinished:^(NSData *data, NSStringEncoding encoding) {
            [post setObject:[UIImage imageWithData:data] forKey:@"image"];
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];

        [teamMemberMeta setObject:connection forKey:@"so_team_member-image_close"];
        [post setObject:connection forKey:@"image"];

        [connection start];
    }
    else if ([memberCloseup isKindOfClass:[UIImage class]]) {
        [[cell imageView] setImage:memberCloseup];
    }

    return cell;
}

修改

您必须将加载逻辑移动到视图控制器。让单元格加载本身是错误的,因为您的单元格正在被重用,因此一个单元格代表各种数据对象和/或图像。我的解决方案可以节省大量内存和数据。