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

时间:2016-06-14 08:45:25

标签: ios objective-c arrays json uitableview

如何在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"
  },

的ViewController

#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;

 }

2 个答案:

答案 0 :(得分:1)

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

-(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;

 }

希望它有用

答案 1 :(得分:0)

从WS获得响应后

dispatch_async(dispatch_get_main_queue(), ^{
                   [self.yourTableview reloadData];     
                        });
  

永远记住当你需要重新加载tableview并且你正在使用WS Call或任何其他块操作时,在主线程中重新加载它