【发布时间】:2016-06-14 08:45:25
【问题描述】:
如何在 UITableView 中重新加载数组数据?我使用 Web 服务并获取存储在数组中的数据。我在 consol 上获得了数据并打印,但无法在 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"
},
视图控制器
#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;
}
【问题讨论】:
-
第一件事,尝试在主线程上重新加载表。其次,确保行数函数返回 > 0。
-
在主线程中重新加载 Tableview
-
谢谢,我在主线程重新加载,然后在表格视图中成功重新加载数据。
-
这行代码你写错了,
name = [retrievedJTrans valueForKey:@"event_name"];我已经更新了你的代码,请检查一下。
标签: ios objective-c arrays json uitableview