我有一个用一个名为json的数组填充的tableview。如果json的长度为0,我想在tableview中返回1行。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [self->tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
NSDictionary *info = [json objectAtIndex:indexPath.row];
//content is the name of label in the custom cell
if ([json count]>0) {
cell.content.text = [info objectForKey:@"message_text"];
}
else
cell.content.text = @"There are currently no messages. Please pull down to refresh";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if([json count]>0)
return [json count];
else
return 1;
}
上面的代码导致我的应用程序崩溃,我不确定为什么。为什么这不起作用,我该如何解决这个问题。 我收到以下错误:
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArray0 objectAtIndex:]:索引0超出空NSArray的边界'
答案 0 :(得分:0)
更改此代码:
NSDictionary *info = [json objectAtIndex:indexPath.row];
//content is the name of label in the custom cell
if ([json count]>0) {
cell.content.text = [info objectForKey:@"message_text"];
}
else
cell.content.text = @"There are currently no messages. Please pull down to refresh";
要:
//content is the name of label in the custom cell
if ([json count]>0) {
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.content.text = [info objectForKey:@"message_text"];
}
else
cell.content.text = @"There are currently no messages. Please pull down to refresh";
希望这有帮助!