gpt4 book ai didi

ios - 只有最后一个 uitableview 单元格是正确的,其余显示空项目

转载 作者:行者123 更新时间:2023-11-28 20:07:54 24 4
gpt4 key购买 nike

基本上我正在学习本教程:http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html由于某种原因,最后一个单元格显示了正确的信息,但前面的单元格只显示标题:

screenshot of ios app

此外,控制台窗口中的输出显示作者的信息信息。所以出于某种原因,它没有被拉入其他牢房。

这是 .m 文件:

#import "ViewController.h"

@interface ViewController ()
{
NSMutableArray *myObject;
// A dictionary object
NSDictionary *dictionary;
// Define keys
NSString *title;
NSString *thumbnail;
NSString *author;
NSString *permalink;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
title = @"title";
thumbnail = @"thumbnail";
author = @"author";
permalink = @"permalink";

myObject = [[NSMutableArray alloc] init];

NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://ios-blog.co.uk/?feed=json"]];

id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];

for (NSDictionary *dataDict in jsonObjects) {
NSString *title_data = [dataDict objectForKey:@"title"];
NSString *thumbnail_data = [dataDict objectForKey:@"thumbnail"];
NSString *author_data = [dataDict objectForKey:@"author"];
NSString *permalink_data = [dataDict objectForKey:@"permalink"];

NSLog(@"TITLE: %@",title_data);
NSLog(@"THUMBNAIL: %@",thumbnail_data);
NSLog(@"AUTHOR: %@",author_data);
NSLog(@"URL: %@",permalink_data);

dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
title_data, title,
thumbnail_data, thumbnail,
author_data,author,
permalink_data,permalink,
nil];
[myObject addObject:dictionary];
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return myObject.count;
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

NSMutableString *text;
//text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:title]];
text = [NSMutableString stringWithFormat:@"%@",
[tmpDict objectForKeyedSubscript:title]];

NSMutableString *detail;
detail = [NSMutableString stringWithFormat:@"Author: %@ ",
[tmpDict objectForKey:author]];

NSMutableString *images;
images = [NSMutableString stringWithFormat:@"%@ ",
[tmpDict objectForKey:thumbnail]];

NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];

cell.textLabel.text = text;
cell.detailTextLabel.text= detail;
cell.imageView.frame = CGRectMake(0, 0, 80, 70);
cell.imageView.image =img;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"URL: %@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]);

NSString* launchUrl = (@"%@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

任何想法为什么以及如何解决这个问题?提前致谢:)

编辑:解决此解决方案后,感谢 Martin R 提供的答案:如果任何 dictionaryWithObjectsAndKeys 值为 nil,我如何提供默认值?

最佳答案

问题出在这里:

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
title_data, title,
thumbnail_data, thumbnail,
author_data,author,
permalink_data,permalink,
nil];

您的 JSON 数组中的许多对象没有“缩略图”条目,并且 thumbnail_datanil

但是 nil 充当 dictionaryWithObjectsAndKeys:终止符,因此,以下键“author”和“permalink”将被忽略。

有多种方法可以解决这个问题。例如,你可以使用 dictionaryWithValuesForKeys: 而不是 dictionaryWithObjectsAndKeys::

for (NSDictionary *dataDict in jsonObjects) {
NSDictionary *dictionary = [dataDict dictionaryWithValuesForKeys:@[title, thumbnail, author, permalink]];
[myObject addObject:dictionary];
}

此方法将 NSNull 值替换为 nil 值(并且代码更少)。

然后,在 cellForRowAtIndexPath 中,检查该值是否为 NSNull:

NSDictionary *tmpDict = dataDict[indexPath.row];
NSString *thumbnailURL = tmpDict[thumbnail];
UIImage *img;
if ([thumbnailURL isEqual:[NSNull null]]) {
img = [UIImage imageNamed:@"DefaultImage.png"];
} else {
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
}

关于ios - 只有最后一个 uitableview 单元格是正确的,其余显示空项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com