我試圖從字段「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;
}
)
have you try [this](http://stackoverflow.com/questions/13534502/ios-loadnibnamed-confusion-what-is-best-practice)? –