2016-06-14 8 views
0

データリロードをUITableViewに配列する方法は?私は、Webサービスを使用し、配列に格納されたデータを取得します。私はデータを取得し、コンソールで印刷しますが、同じアレイはTableViewでリロードできません。UITableViewCellで配列データを表示する方法は?

何度も試してみましたが、データを表示できないテーブルビューでデータを表示する方法を教えてください。

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; 

} 
+0

役立つ最初のものを願って、上の表をリロードしてみてください、コードの下にこれを使用して、あなたは正しく配列値を設定していない隣、テーブルビューをリロードすることはできませんですメインスレッド。第2に、行数が> 0を返すことを確認してください。 – Mahesh

+0

メインスレッドのテーブルビューを再ロード –

+0

ありがとうございます。メインスレッドにリロードしてから、テーブルビューでデータを正常にリロードします。 –

答えて

0

あなたはWS

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

からの応答を取得した後は、テーブルビューをリロード必要があり、WSコールまたはを使用している場合は、必ずということを覚えていますそれ以外のブロック操作でメインスレッドにリロードする

+0

返信ありがとう、ありがとうございます。 –

1

あなたのミスが

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

} 

はその

関連する問題