Вопрос прост: как загрузить пользовательский UITableViewCell
из файлов Xib? Это позволяет вам использовать Interface Builder для проектирования ваших ячеек. Ответ очевидно не прост из-за проблем управления памятью. Этот поток упоминает проблему и предлагает решение, но является предварительным выпуском NDA и испытывает недостаток в коде. Здесь длинная ветка, в которой обсуждается проблема без предоставления однозначного ответа.
Вот некоторый код, который я использовал:
static NSString *CellIdentifier = @"MyCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCell *)[nib objectAtIndex:0];
}
Чтобы использовать этот код, создайте MyCell.m/.h, новый подкласс UITableViewCell
и добавьте IBOutlets
для компонентов, которые вы хотите. Затем создайте новый файл "Пустой XIB". Откройте файл Xib в IB, добавьте объект UITableViewCell
, установите для его идентификатора значение "MyCellIdentifier", установите для его класса значение MyCell и добавьте свои компоненты. Наконец, подключите IBOutlets
к компонентам. Обратите внимание, что мы не установили владельца файла в IB.
Другие методы рекомендуют устанавливать владельца файла и предупреждают об утечках памяти, если Xib не загружается через дополнительный фабричный класс. Я проверил вышеизложенное в разделе "Инструменты/утечки" и не обнаружил утечек памяти.
Так какой же канонический способ загрузки ячеек из Xibs? Мы устанавливаем владельца файла? Нужен ли нам завод? Если да, то как выглядит код фабрики? Если есть несколько решений, давайте уточним плюсы и минусы каждого из них...