0

我在 SO 上看到了几篇关于此的帖子,但这些解决方案似乎对我不起作用。也许是因为我通过 JSON 获取图像 URL。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;
}
4

2 回答 2

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 上发布另一个关于最佳解决方案的问题。

于 2012-09-29T15:42:55.193 回答
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}
}
于 2012-09-29T05:57:32.493 回答