Я в настоящее время изучаю UITableViewCell с помощью книги. Чтобы повторно использовать ячейки при прокрутке, автор просит изменить исходный код, включив в него оператор if ()
, чтобы проверить, существует ли ячейка с конкретным идентификатором повторного использования. Однако после добавления оператора if ()
Xcode выдает предупреждение, которое говорит Неиспользуемая переменная 'cell'
в строке внутри if (! Cell)
. При запуске кода я получаю сообщение об ошибке Завершение работы приложения из-за неперехваченного исключения «NSInternalInconsistencyException», причина: «UITableView dataSource должен возвращать ячейку из tableView: cellForRowAtIndexPath:»
Что-то пошло не так?
Исходный код
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"] autorelease];
Possession *p = [[[PossessionStore defaultStore] allPossessions] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
return cell;
}
Измененный код
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Check for reusable cell first, use that if it exists
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
// If there is no reusable cell of this type, create a new one
if (!cell) {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"] autorelease];
}
Possession *p = [[[PossessionStore defaultStore] allPossessions] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
return cell;
}