检索到数据但表格单元格UILabel为空?

时间:2015-11-24 16:30:16

标签: ios objective-c uitableview

我正在使用以下代码从端点检索实体数据(我在Drupal的iOS SDK上构建了我的应用程序),并且我正在尝试在我的TableView单元格中显示它。

那就是说,当我使用下面的代码在我的单元格中显示它时,它似乎不想工作(标签只显示为空)?请参阅下面的控制台数据。

TableViewController.h

@property (strong, nonatomic) NSMutableArray *messages;

TableViewController.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 *)response;

                 NSLog(@"This is all of the data from response %@", response); }


             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 *receivedMessages = [self.messages objectAtIndex:indexPath.row];
        [[cell subjectLine] setText:[receivedMessages objectForKey:@"field_message_body"]];
        NSLog(@"Received message subject are here %@", receivedMessages);
       return cell;

    }

CONSOLE DATA返回为:

2015-11-23 18:26:47.998 [624:153845] This is all of the data from response {
    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;
            }
        );
    };

3 个答案:

答案 0 :(得分:0)

您需要更好地管理响应数据。正如@limfinity所说,responseData是NSDictinary,而不是NSMutableArray

此外,您应在self.messages中设置消息后重新加载tableview,以便该表从“&#39; new&#39;中获取新数据。 self.messages

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

     NSLog(@"This is all of the data from response %@", response); }
     dispatch_async(dispatch_get_main_queue(), ^(void){
         [self.tableView reloadData];
     });


 failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"failed to get data"); }
];

修改

只是做一个简单的(但不是那么正确的)改变来使这项工作并且不会崩溃,你应该说:

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

而不是:

self.messages = (NSMutableArray *)response;

但我认为你应该更深入,让答案成为NSArray,而不只是一个NSDictionary(多个NSDictionaries,而不只是一个)

答案 1 :(得分:0)

在获得响应之前,tableView已完成加载。你可以记录一下 numberOfRowsInSection:cellForRowAtIndexPath方法查看是否在"This is all of the data from response ..."之前。

您可以在主线程上调用[self.tableview reloadData]以在收到消息后重新加载tableview。

答案 2 :(得分:0)

根据您的控制台日志:

2015-11-23 18:26:47.998 [624:153845] This is all of the data from response {
    arguments =     (
    );
    data =     (
    );
    "field_message_body" =     {//This is not a String!
        und =         (
                        {
                format = "<null>";
                "safe_value" = "Testing message center";
                value = "Testing message center";
            }
        );
    };

[receivedMessages objectForKey:@"field_message_body"]不是字符串,而是字典。

您需要再解析该对象:

NSString *value = receivedMessages[@"field_message_body"][@"und"][@"value"];
[[cell subjectLine] setText:value];

并且在获取数据后也不要忘记重新加载tableview。