【问题标题】:how to show array data on UITableViewCell?如何在 UITableViewCell 上显示数组数据?
【发布时间】: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


【解决方案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;

 }

希望对你有帮助

【讨论】:

    【解决方案2】:

    收到 WS 的回复后

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      相关资源
      最近更新 更多