UITableView -headerViewForSection возвращает (null)
у меня естьUITableView
с 2 разделами. Каждый со своимheaderView
.
Я создал кастомheaderView
через-viewForHeaderInSection:
метод.
Позже я планирую немного его изменить, поэтому мне нужно использоватьviewForHeader
метод, но я не могу получить доступ кheaderView
и этоsubViews
.
В качестве простого примера я пытаюсьNSLog
viewForHeader
объект в-didSelectRowAtIndexPath:
но я получаю(null)
результат.
Образец кода:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 75;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *myHeader = [[UIView alloc] init];
switch (section) {
case 0:
[myHeader setBackgroundColor:[UIColor greenColor]];
break;
case 1:
[myHeader setBackgroundColor:[UIColor redColor]];
break;
default:
break;
}
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setFrame:CGRectMake(10, 0, 100, 30)];
[myLabel setTag:101];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:[NSString stringWithFormat:@"Section: %d",section]];
[myHeader addSubview:myLabel];
return myHeader;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewHeaderFooterView *testView = [self.tableView headerViewForSection:indexPath.section];
NSLog(@"%@",testView); //displays (null)
}
Нужно ли создавать кастомыUIView
XIB какheaderView
? (потому что согласноаналогичный вопрос и согласно документам«)
Чтобы табличное представление знало о вашем верхнем или нижнем колонтитуле, вам нужно зарегистрировать его.
Вы делаете это с помощью registerNib: forCellReuseIdentifier: или
registerClass: forCellReuseIdentifier: метод UITableView.