Ответ 1
-(void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"cell" bundle:nil]
forCellReuseIdentifier:@"cell"];
}
static NSString *cellIdentifier = @"cell";
if (tableView ==tableview1)
{
ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell1 == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ContactCustom" owner:self options:nil];
cell1 = contactCustom;
}
}
Как зарегистрировать имя nib в методе viewDidLoad
перед вызовом метода cellForRowAtIndex
?
-(void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"cell" bundle:nil]
forCellReuseIdentifier:@"cell"];
}
Apple предоставила метод регистров для UITableView после IOS 5 Пожалуйста, ознакомьтесь со ссылкой на класс http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Пример:
In view did load you can register nib for UITableView like below
[tableView registerNib:[UINib nibWithNibName:@"nibName" bundle:nil] forCellReuseIdentifier:@"identifienName"];
In cellForRowAtIndexPath
cell = [tableView dequeueReusableCellWithIdentifier:@"identifienName"];
(void)viewDidLoad
{
[super viewDidLoad];
[_tbl_setpaper registerNib:[UINib nibWithNibName:@"SetPaperCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
}
Просто поместите oneline в свою viewdidload, у вас должны быть выход таблицы, идентификатор ячейки, класс ячейки.
static NSString *cellIdentifier = @"cell";
if (tableView ==tableview1)
{
ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell1 == nil)
{
cell1 = [tableview dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}
}
// First of all Declare an IBOutlet for your customCell in Your View Controller
IBOutlet ScoreCell *scoreCell;
// Synthesize it.
// Assign your view controller class to File Owner of your custom cell (Eg. File Owner of ScoreCell.xib)
// Then Assign Reference Outlet of ScoreCell.xib with Object 'scoreCell'
// Finally Create your custom Cell as follow :
ScoreCell *cell = (ScoreCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Регистрирует объект nib, содержащий ячейку с представлением таблицы под указанным идентификатором.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
Parameters
СИБ Объект nib, который указывает файл nib, который будет использоваться для создания ячейки. Этот параметр не может быть равен нулю. идентификатор Идентификатор повторного использования для ячейки. Этот параметр не должен быть nil и не должен быть пустой строкой.
Этот doc может вам помочь.
Смотрите здесь: http://mrmaksimize.com/Custom-UITableViewCell-With-NIB/
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"EXCustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCellReuseID"];
}
Позже в коде:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellReuseID";
EXCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
[cell.cellItemImage setImage:[UIImage imageNamed:@"glyphicons_428_podium"]];
[cell.cellItemLabel setText = @"Mr Burns."];
return cell;
}
UINib * cellNib = [UINib nibWithNibName: @"Custom_cellTableViewCell" bundle: nil]; [self.tableView registerNib: cellNib forCellReuseIdentifier: @"Custom_cellTableViewCell"];
Этот код работает нормально
Для Свифта
tableViewSubCategory.register(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: "")