2016-03-02 9 views
-1

這是一個響應,我想打印這個響應值到表視圖: 我張貼我的tableview代碼請檢查是否正確或錯誤。 我在這裏掙扎了2天,如果有人知道請回復我。我的表視圖創建但數組沒有顯示在表視圖,在控制檯中所有的數據都come.please幫我解決json解析

- (IBAction)search:(UIButton *)sender { 


NSString *post =[[NSString alloc]initWithFormat:@"keyword=%@",[keyword text]]; 
NSLog(@"PostData: %@",post); 

NSURL *url=[NSURL URLWithString:@"url"]; 

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

//NSError *error = [[NSError alloc] init]; 
NSError *error=nil; 
NSHTTPURLResponse *response = nil; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
if (!urlData) { 
    NSLog(@"Error: %@", [error localizedDescription]); 
    //return NO; 
} 


NSLog(@"Response code: %ld", (long)[response statusCode]); 



if ([response statusCode] >=200 && [response statusCode] <300) 
{ 
    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response ==> %@", responseData); 

    NSError *error = nil; 
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:urlData 
         options:NSJSONReadingMutableContainers error:&error]; 
    NSLog(@"json ==> %@ ", dic); 
    name  = [[NSMutableArray alloc]init]; 
    username = [[NSMutableArray alloc]init]; 

    for (id obj in dic) { 
     NSString *nameNameObj=[obj objectForKey:@"name"]; 
     [name addObject:nameNameObj]; 
     NSString *userNameObj=[obj objectForKey:@"username"]; 
     [username addObject:userNameObj]; 

    } 

    NSLog(@"name array is %@",name); 
    NSLog(@"username array is %@",username); 

    } 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return [name count]; 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    return [name count]; 
    } 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
    (NSIndexPath *)indexPath 
    { 
    static NSString *simpleTableIdentifier = @"MYCustomCell"; 
    MyCustomCellTableViewCell *cell = (MyCustomCellTableViewCell *) 
    [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
    NSArray *nib = [[NSBundle mainBundle]  
    loadNibNamed:@"MyCustomCellTableViewCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    } 
    // apply setters for cell 
    cell.nameLbl.text=[name objectAtIndex:indexPath.row]; 
    cell.usernameLbl.text=[username objectAtIndex:indexPath.row]; 
    return cell; 
} 
[ 
    {"id":60,"username":"test001","name":"name"},  
    {"id":61,"username":"test","name":""}, 
    {"id":65,"username":"test002","name":"first"}, 
    {"id":69,"username":"test003","name":"second"}, 
    {"id":71,"username":"test004","name":"third"}, 
    {"id":78,"username":"test005","name":"fourth"}, 
    {"id":79,"username":"test006","name":"Name of the person"} 
] 
+0

把你的代碼????? –

+0

請在此處添加您的tableview委託方法。沒有這些,我們不能幫助你。 – Rashad

回答

0

忽略這一點,因爲你是不使用本

檢查這2行:

cell.nameLbl.text = [name objectAtIndex:indexPath.row]; 
cell.usernameLbl.text = [username objectAtIndex:indexPath.row]; 

name是一個數組,包含的NSDictionary中的每個索引。所以,如果你要訪問它的價值,你必須這樣做:

cell.nameLbl.text = [[name objectAtIndex:indexPath.row] objectForKey:@"name"]; 
cell.usernameLbl.text = [[username objectAtIndex:indexPath.row] objectForKey:@"username"]]; 

編輯:

正如你所編輯的代碼,修改後的答案是:

- (IBAction)search:(UIButton *)sender { 


    NSString *post =[[NSString alloc]initWithFormat:@"keyword=%@",[keyword text]]; 
    NSLog(@"PostData: %@",post); 

    ...... 
    ...... 

    if ([response statusCode] >=200 && [response statusCode] <300) 
    { 
     NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
     NSLog(@"Response ==> %@", responseData); 
     ..... 
     ..... 

     for (id obj in dic) { 
      NSString *nameNameObj=[obj objectForKey:@"name"]; 
      [name addObject:nameNameObj]; 
      NSString *userNameObj=[obj objectForKey:@"username"]; 
      [username addObject:userNameObj]; 

     } 

     ....... 
     ....... 
     [self.tableView reloadData]; 

    } 
} 

希望這有助於... :)

+0

沒有它的不工作。再次數據進入控制檯,但沒有顯示在表格視圖.. –

+0

@ParveshMishra>你的名字和用戶名數組有數據嗎? – Rashad

+0

2016年3月2日15:38:23.107 UITableView1 [7722:105442]名稱陣列( vfvv, 「」, 「」, 「富蘭克林」, vneflv, 「富蘭克林」, 「名稱38:23.108 UITableView1 [7722:105442]的人」 ) 2016年3月2日15的用戶名是陣列( test001, 測試, test002, test003, test004, test005, test006 ) –

0

可能有以下一些原因:

  1. 請務必設置委託和數據源正常。

  2. 當您收到的主線程

    dispatch_async(dispatch_get_main_queue(), ^{                 
    
         [self.tableView reloadData]; 
    }); 
    
  3. 返回有效的細胞反應重載的tableView從細胞的

    cellForRowAtIndexPath: 
    
  4. 返回有效的數量從

    numberOfRowInSection: 
    
+0

其中我把調度代碼放入.m文件 –

相關問題