gpt4 book ai didi

ios - 如何在 UITableViewCell 上显示数组数据?

转载 作者:行者123 更新时间:2023-11-29 00:54:00 25 4
gpt4 key购买 nike

如何在 UITableView 中重新加载数组数据?我使用网络服务并获取存储在数组中的数据。我得到了一个数据并在 consol 上打印,但相同的数组无法在 TableView 中重新加载。

请帮助我如何在 TableView 上显示数据,我尝试了很多次但无法显示数据。

JSON

{
"status": {
"event": [
{
"event_id": "28",
"event_name": "Event name",
"event_title": "svsav",
"img": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/1461402793-Vtcg8pRB.jpg",
"event_date": "2016-04-05",
"event_time": "12:00",
"event_detail": "svsa"
},

View Controller

#import "ViewController.h"
#import "celltable.h"

@interface ViewController ()
{

NSArray*name;

}

@end


- (void)viewDidLoad {
[super viewDidLoad];
[self.tableview setAllowsMultipleSelection:YES];

NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_event.php"]];
response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[response appendData:data];
NSLog(@"error receving data %@",response);
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;

NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"response data %@",son);

NSError *jsonParsingError = nil;
NSDictionary *retrievedJTransD = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray* retrievedJTrans = retrievedJTransD[@"status"][@"event"];
name = [retrievedJTrans valueForKey:@"event_name"];
NSLog(@"success %@", name);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"tableview cell");
celltable *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

cell.date.text=[NSString stringWithFormat:@"%@",[name objectAtIndex:indexPath.row]];

return cell;

}

最佳答案

你的错误是无法重新加载表格 View ,接下来你没有正确设置数组值,使用下面的代码,

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;

NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"response data %@",son);

NSError *jsonParsingError = nil;
NSDictionary *retrievedJTransD = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray* retrievedJTrans = retrievedJTransD[@"status"][@"event"];

//you are wrong in name array change it.

name = retrievedJTrans;
NSLog(@"success %@", name);

[self.tableview reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"tableview cell");
celltable *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

cell.date.text=[NSString stringWithFormat:@"%@",[[name objectAtIndex:indexPath.row] valueForKey:@"event_name"]];

return cell;

}

希望对你有帮助

关于ios - 如何在 UITableViewCell 上显示数组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807090/

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