私はここ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はサブタイトルであり、他の変更や配線はありません。
私がめちゃくちゃになっているアイデアはありますか?
async
PSリクエストを使用した私の完全なコード
- (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;
}