在iOS中从url设置UITableview单元格图像

时间:2014-03-06 16:34:49

标签: iphone uitableview ios7

我正在使用图片网址在UITableViewCell上设置图片,但是图片不会加载,除非我滚动UITableView.Here是我一直在使用的代码,我知道SDWeb图像库从网址下载图像,但我确实想要使用任何库。请告诉我在这方面做错了什么目的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...

    NSDictionary *dictCard=[cardsDetailsArray objectAtIndex:indexPath.row];

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:10];
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:11];
    UIImageView *cellImage=(UIImageView *)[cell viewWithTag:0];


    CGSize firstSize=CGSizeZero;
    if([[dictCard objectForKey:@"orientation"]isEqualToString:@"1"]){
        [cellImage setFrame:CGRectMake(0, 0, 75, 68)];
        firstSize=CGSizeMake(75,68);
    }
    if([[dictCard objectForKey:@"orientation"]isEqualToString:@"0"]){
        [cellImage setFrame:CGRectMake(0, 0, 107, 72)];

        firstSize=CGSizeMake(107,72);
    }
    [cellImage setBackgroundColor:[UIColor clearColor]];
    NSString *string=[dictCard objectForKey:@"email_sent"];
    NSArray *items = [string componentsSeparatedByString:@","];



    dateLabel.text=[dictCard objectForKey:@"date"];
    nameLabel.text=[NSString stringWithFormat:@"Sent to %d People",[items count]];


    NSURL* url = [NSURL URLWithString:(NSString *) [dictCard objectForKey:@"card_thumbnail"]];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];


    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   UIImage* image = [[UIImage alloc] initWithData:data];
//i am scaling the image here
                                        UIImage *imageScale=[self imageWithImage:image scaledToSize:firstSize];
                                        [cellImage setImage:imageScale];
                               }

                           }];




    return cell;
}

我还添加了[__tableViewDraft reloadData];设置了单元格图像后,由于此原因,我的单元格不断更改其单元格图像。

1 个答案:

答案 0 :(得分:0)

下载图像后,您需要让iOS刷新单元格。查看reloadRowsAtIndexPaths:withRowAnimation:等。

通常,您可能拥有CoreData对象支持的单元格,并下载并将图像保存到托管对象。然后,当托管对象更改时,您会有NSFetchedResultsControllerDelegate触发更新,刷新该行。

相关问题