【发布时间】:2014-01-02 12:04:04
【问题描述】:
我正在尝试使用此代码获取节标题视图:
[tableView headerViewForSection:indexPath.section];
但是这段代码总是返回零。 你能给我一些例子来从表格视图中获取部分标题视图吗? 这是我的 viewForHeader 实现:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
DynamicHeader *headerView = [[DynamicHeader alloc] init];
headerView.frame = CGRectMake(0, 0, 320, 40);
UILabel *headerLbl = [[UILabel alloc] init];
headerLbl.frame = CGRectMake(10, 10, 300, 20);
if(((SectionViewController *)sharedInstance.currentViewController).currentSectionType == 25)
{
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minus.png"]];
imgView.frame = CGRectMake(285, 14.5, 16, 13);
[headerView addSubview:imgView];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:sharedInstance action:@selector(headerTap:)];
[headerView addGestureRecognizer:recognizer];
headerView.tableView = tableView;
headerView.heightForRows = 95;
headerView.isOpen = YES;
}
headerLbl.text = [[[[[((SectionViewController *)sharedInstance.currentViewController).responseDictionary valueForKey:DATA_PARAMETER] valueForKey:SECTION_TABLE_PARAMETER] objectAtIndex:section] valueForKey:TABLE_SECTION_HEADER] capitalizedString];
[headerView addSubview:headerLbl];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((DynamicHeader *)[tableView headerViewForSection:indexPath.section] != nil)
{
return ((DynamicHeader *)[tableView headerViewForSection:indexPath.section]).heightForRows;
}
else
{
return 95;
}
}
- (void)headerTap: (UITapGestureRecognizer *)recognizer
{
DynamicHeader *view = (DynamicHeader *)[recognizer view];
if(view.isOpen)
{
view.heightForRows = 0;
}
else
{
view.heightForRows = 95;
}
view.isOpen = !view.isOpen;
[view.tableView beginUpdates];
[view.tableView endUpdates];
}
【问题讨论】:
-
你是否使用定义的 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 来分配视图?
-
你对
headerViewForSection:的实现是什么(如果有的话)? -
@rokjarc 我更新了我的问题。
-
在这种情况下,您的
tableview对象在您调用此方法时似乎是nil。或者它的delegate尚未设置(由您)。因为tableview调用它的delegate方法 - 它没有实现自己的。 -
@rokjarc,目前我正在调用此方法 tableview 不是零。另外,我设置了 tableview 委托和数据源。
标签: ios objective-c uitableview