Для тех из нас, кто использует более новую версию (iOS 6 и выше) UITableView
API для удаления из очереди ячеек, а именно dequeueReusableCellWithIdentifier:forIndexPath
это фактически гарантированно возвращает инстанцированную ячейку, поэтому мы не можем выполнить проверку nil и вручную вызвать initWithStyle
.Поэтому лучшим решением является создание подкласса UITableViewCell
и принудительное применение стиля при инициализации.
Так, в качестве примера, если мы хотели бы иметь ячейку со стилем UITableViewCellStyleSubtitle
, мы бы создали собственный подкласс:
@interface ABCSubtitledTableViewCell : UITableViewCell
@end
@implementation ABCSubtitledTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
@end
И тогда в нашем viewDidLoad
мы зарегистрируем соответствующий класс
[tableView registerClass:[ABCSubtitledTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ABCSubtitledTableViewCell class])];
Создание нашего метода dequeue:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ABCSubtitledTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ABCSubtitledTableViewCell class]) forIndexPath:indexPath];
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = @"Hello";
cell.detailTextLabel.text = @"World";
return cell;
}