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

My Table Cellはサブタイトルであり、他の変更や配線はありません。

私がめちゃくちゃになっているアイデアはありますか?

asyncPSリクエストを使用した私の完全なコード

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

    [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 に答える
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 に答える