gpt4 book ai didi

ios - 在 UITableView 中显示 Json 结果

转载 作者:行者123 更新时间:2023-11-28 18:00:21 44 4
gpt4 key购买 nike

我正在尝试将我的 JSON 结果放入我的 UITableView 中。我得到了 JSON 但我无法将它们放入我的 tableview。知道我做错了什么吗?

这里是一些相关的代码:

- (void)viewDidLoad {
TitlosArray = [[NSMutableArray alloc] init];
KeimenoArray = [[NSMutableArray alloc] init];
[super viewDidLoad];
[self fetchTweets];
}

我的 JSON 解析器工作正常:

- (void)fetchTweets {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myselection.gr/support3/frontistiria_project/android_data.php/?type=publicNews"]];

[self performSelectorOnMainThread:@selector(fetchedForecast:)withObject:data waitUntilDone:YES];
});
}

- (void)fetchedForecast:(NSData *)responseData {
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);
NSArray *items = [json valueForKeyPath:@"content"];

NSEnumerator *enumerator = [items objectEnumerator];
titlosjson.text=[[items objectAtIndex:1] objectForKey:@"title"];
Keimenojson.text=[[items objectAtIndex:1] objectForKey:@"descr"];

while (item = (NSDictionary*)[enumerator nextObject]) {
[TitlosArray addObject:[item objectForKey:@"title"]];
[KeimenoArray addObject:[item objectForKey:@"descr"]];
NSLog(@"Title = %@", [item objectForKey:@"title"]);
NSLog(@"Descr = %@",[item objectForKey:@"descr"]);
}

NSLog(@"%@",[TitlosArray objectAtIndex: 1]);
myArray = [NSArray arrayWithArray:TitlosArray ];
NSLog(@"%@", [myArray objectAtIndex: 2]);
}

但是我的 UITableView 有问题:

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

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

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

//cell.textLabel.text = [NSString stringWithFormat:@"by %@", text];
return cell;
}

最佳答案

您正在创建空单元格,因此不会显示您的内容。

尝试用这个替换你的 cellForRowAtIndexPath: 方法:

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

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

cell.textLabel.text = [TitlosArray objectAtIndex:indexPath.row];

return cell;
}

关于ios - 在 UITableView 中显示 Json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16150207/

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