2016-01-10 3 views
0

У меня есть табличное представление, которое заполнено массивом с именем json. Если длина json равна 0, я хотел бы вернуть 1 строку в моем представлении таблицы.Сбой приложения при возврате 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

Пожалуйста, напишите свой метод 'cellForRowAtIndexPath' – Andrew

+0

Я добавил метод cellForRowAtIndexPath @Andrew – John55

ответ

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"; 

To:

//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"; 

Надеется, что это помощь!

+0

Да, это сработало. Огромное спасибо. Просто из любопытства, почему возникла проблема и почему код, который вы предоставили, работает. Я просто пытаюсь понять это. – John55

+0

Вы получаете элемент за пределами своего кода проверки. Когда ни один элемент не выходит за пределы диапазона, и он выйдет из строя. –

Смежные вопросы