2016-02-02 43 views
-1

我試圖從字段「field_message_subject」和「field_message_body」中將JSON數據拉到我的應用中的表單元格中。我使用下面的代碼來完成它,並且數據成功返回,但是我的應用程序崩潰並且標籤沒有填充。崩潰的錯誤是:在帶有返回的JSON的TableView單元格中填充UILabel

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

Viewcontroller.m

- (void)viewDidLoad { 
     [super viewDidLoad]; 


     NSDictionary *entityData = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"1"] forKey:@"uid"]; 

     [DIOSEntity 
     entityGet:entityData 
     name:@"entity_message" 
     eid:@"uid" 
     success:^(AFHTTPRequestOperation *op, id response) { 
      self.messages = [NSMutableArray arrayWithObject:(NSDictionary*)response]; 

      dispatch_async(dispatch_get_main_queue(), ^(void){ 
        [self.tableView reloadData]; 
      }); 
     } 
     failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"failed to get data"); } 
     ]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    static NSString *PointsTableIdentifier = @"MyMessagesCell"; 

    MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

    } 

    NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row]; 
    [[cell subjectLine] setText:[receivedSubjectLine objectForKey:@"field_message_subject"]]; 
    NSLog(@"Received message subject is here %@", receivedSubjectLine); 

return cell; 

} 

請參見下面的返回JSON格式:

2016-02-02 10:37:19.996 app[4208:1406692] Messages are as follows (
      { 
      arguments =   (
      ); 
      data =   (
      ); 
      "field_message_body" =   { 
       und =    (
            { 
         format = "<null>"; 
         "safe_value" = "Testing message center"; 
         value = "Testing message center"; 
        } 
       ); 
      }; 
      "field_message_group_ref" =   (
      ); 
      "field_message_subject" =   { 
       und =    (
            { 
         format = "<null>"; 
         "safe_value" = Testing; 
         value = Testing; 
        } 
       ); 
      }; 
      "field_message_user_ref" =   (
      ); 
      language = en; 
      mid = 1; 
      "rdf_mapping" =   (
      ); 
      timestamp = 1447260780; 
      type = "private_message"; 
      uid = 1; 
     } 
    ) 
+0

have you try [this](http://stackoverflow.com/questions/13534502/ios-loadnibnamed-confusion-what-is-best-practice)? –

回答

1

根據你的JSON格式,訪問field_message_subject應該是類似於

[[[[receivedSubjectLine objectForKey:@"field_message_subject"] objectForKey:@"und"] objectAtIndex:0] objectForKey:@"value"]; 
+0

這看起來更接近我想要的,但我怎麼把細胞放在這一行(我想填充的UILabel稱爲subjectLine,它位於我的TableView單元格中)? – Brittany

+0

像你這樣在cellForRowAtIndexPath中使用它。您可以將上述設置爲名爲messageSubject的NSString,位於NSDictionary * receivedSubjectLine = [self.messages objectAtIndex:indexPath.row]下。然後,[[cell subjectLine] setText:messageSubject]。 –

0

你加入response(這是NSDictionary)至self.messages。您的代碼中沒有其他地方插入self.messages。因此messages數組將始終保存一個值。您正試圖從messages陣列的行索引獲取對象。因此,如果行數超過1,您的應用程序將崩潰,因爲messages只包含1個對象。

+0

目前,我已經設置tableView只返回一行? – Brittany

+0

您的崩潰消息表示它正在嘗試訪問索引1,這將是您表中的第二行。你確定你只是告訴它有一排嗎? – dan

+0

我已將它更改爲:我已添加return [self.messages count];但不應該我上面突出顯示的塊看起來不同,以檢索指定的字段?例如。如:... objectForKey:@「und」] objectForKey:@「value」]; ?我只是不確定代碼的外觀如何? – Brittany

1

tableview應該知道它需要多少個單元格。 定義此tableviewdatasource方法來設置所需的行數。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.messages count]; 
} 

的,如果你增加行的,你應該初始化self.messages在viewDidLoad並添加條目withh addObject:數量的方式;

self.messages = [NSMutableArray arrayWithObject:(NSDictionary*)response]; 

,因爲這將永遠爲您的陣列分配新的空間,並且其規模將始終爲1

編輯

NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row]; 

    NSArray * val = receivedSubjectLine[@"und"]; 
    NSString *textToWrite = val[0][@"value"]; 
     [cell.subjectLine setText:textToWrite]; 

寫在該領域的文本value

+0

我已經添加了return [self.messages count],但現在控制檯拋出錯誤:***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSCFDictionary length]:無法識別的選擇器發送到實例0x132b87040 '當我打我的ViewController ... – Brittany

+0

bacause你的'field_message_subject'不是NSString它也'NSDictionary' – meth

+0

所以不應該我強調的代碼行看起來像這樣︰... objectForKey:@「und」] objectForKey :@「值」]; ?我只是不確定代碼的外觀如何? – Brittany

相關問題