无法在iOS中显示Table Cell左侧的缩略图

时间:2012-09-28 22:06:38

标签: objective-c ios nsstring uiimage nsdata

我在SO上看到了几个帖子,但这些解决方案对我来说似乎不起作用。也许是因为我通过JSON获取图片网址。 JSON中的所有其他文本字段都通过OK,它只是我无法显示的图像并且收到SIGABRT错误。

有问题的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PostCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *post           = [posts objectAtIndex:indexPath.row];
    NSString     *postText       = [post objectForKey:@"post_text"];
    NSString     *postAuthorName = [post objectForKey:@"post_author_name"];
    NSString     *postPictureUrl = [post objectForKey:@"post_picture"];
    NSData       *data           = [NSData dataWithContentsOfURL:[NSURL URLWithString:postPictureUrl]];

    cell.textLabel.text       = postText;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", postAuthorName];
    cell.imageView.image      = [UIImage imageWithData:data];

    return cell;
}

我的表格单元格是副标题,没有其他更改或连线。

知道我搞砸了吗?

PS我使用async请求的完整代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PostCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *post           = [posts objectAtIndex:indexPath.row];
    NSString     *postText       = [post objectForKey:@"post_text"];
    NSString     *postAuthorName = [post objectForKey:@"post_author_name"];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *postpictureUrl = [post objectForKey:@"http://example.com/post/1200"];
        NSData   *data           = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];

//      NSLog(@"%@", data);

        dispatch_async(dispatch_get_main_queue(), ^{
//            [[cell imageView] setImage:[UIImage imageWithData:data]];
        });
    });

    cell.textLabel.text       = postText;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", postAuthorName];

    return cell;
}

2 个答案:

答案 0 :(得分:0)

你可以这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    [NSThread detachNewThreadSelector:@selector(downloadImages:) toTarget:self withObject:  [NSMutableDictionary dictionaryWithObjectsAndKeys:imageURL,@"imageURL",[NSIndexPath indexPathWithIndex:indexPath.row],@"indexPath", nil]];

调用上面的函数和

- (void) downloadImages:(NSMutableDictionary*)dict{

    NSURL *nurl = [NSURL URLWithString:[dict objectForKey:@"imageURL"]];

    NSURLRequest *req = [NSURLRequest requestWithURL:nurl];
    NSURLResponse * response;

    NSData *data = [NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];


    UITableViewCell *cell = [YOURTABLENAME cellForRowAtIndexPath:(NSIndexPath*)[dict objectForKey:@"indexPath"]];

    cell.imageView.image = [UIImage imageWithData:data];

    if (error) {...handle the error}
}

答案 1 :(得分:0)

一些小的改动和下面的工作(即缩略图显示在标题和副标题的左侧)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *post           = [posts objectAtIndex:indexPath.row];

    cell.textLabel.text       = [post objectForKey:@"post_text"];
    cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];

    NSString *postpictureUrl = [post objectForKey:@"picture"];
    NSData   *data           = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];

    cell.imageView.image = [UIImage imageWithData:data];

    return cell;
}

请注意使用initWithStyle:UITableViewCellStyleSubtitle代替Default

这里的指南是一个重要的读物: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html

最后,请注意这是同步下载图像,这在现实世界中不起作用。 所以我将在SO上发布关于最佳解决方案的另一个问题。