ios 如何从 UItableView 获取部分标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20882561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get section header from UItableView
提问by Kisel Alexander
I am trying to get section header view using this code:
我正在尝试使用以下代码获取部分标题视图:
[tableView headerViewForSection:indexPath.section];
but this code always returns me nil. Could you give me some examples to get section header view from table view? This is my viewForHeader implementation:
但这段代码总是返回零。你能给我一些例子来从表格视图中获取部分标题视图吗?这是我的 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];
}
回答by LaborEtArs
Quite old question, but maybe a solution is helpful to others too...
很老的问题,但也许一个解决方案对其他人也有帮助......
An UITableView does create cell, header and footer views only if they are needed (e.g. they are visible in the table view content area).
UITableView 只有在需要时才会创建单元格、页眉和页脚视图(例如它们在表格视图内容区域中可见)。
If the cell, header or footer view isn't visible in the table view, a call to 'cellForRowAtIndexPath:', 'headerViewForSection:' or 'footerViewForSection:' might return 'nil'. (See UITableView.h for documentation of this behaviour. The only exception would be, that the view has not jet been recycled by the table view)
如果单元格、页眉或页脚视图在表格视图中不可见,则调用“ cellForRowAtIndexPath:”、“ headerViewForSection:”或“ footerViewForSection:”可能会返回“ nil”。(有关此行为的文档,请参阅 UITableView.h。唯一的例外是,该视图尚未被表视图回收)
Of course you can create any table view subview by calling the responsible delegate method directly, but UITableView wouldn't do so by itself.
当然,您可以通过直接调用负责的委托方法来创建任何表视图子视图,但 UITableView 本身不会这样做。
So the solution to access a cell, header oder footer view is to make it visible in the table view first.
因此,访问单元格、页眉或页脚视图的解决方案是首先使其在表格视图中可见。
An example for an header view:
标题视图的示例:
CGRect sectionHeaderRect = [self.tableView rectForHeaderInSection:groupSectionIndex];
[self.tableView scrollRectToVisible:sectionHeaderRect
animated:NO];
// Let the tableview load the needed views
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewHeaderFooterView* groupSectionHeaderView = [self.tableView headerViewForSection:sectionIndex];
// Use groupSectionHeaderView
});
回答by Sean Chen
Use this delegate method to access the header view:
使用此委托方法访问标题视图:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
回答by Nikita
Have you implemented
你实施了吗
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and
和
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
in your ViewController subclass?
在您的 ViewController 子类中?
And if yes, probably you have some problem in one of these methods.
如果是,那么您可能在其中一种方法中遇到了问题。
回答by Keith Kennedy
Where are you calling [tableView headerViewForSection:indexPath.section];
?
你在哪儿打电话[tableView headerViewForSection:indexPath.section];
?
If you are calling it in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
before the tableView is visible then you will get nil back as the tableView is not setup yet.
如果您- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在 tableView 可见之前调用它,那么您将返回 nil,因为 tableView 尚未设置。
If you call it else where once the tableView is loaded, you should get a UITableViewHeaderFooterView
back.
如果在加载 tableView 后在其他位置调用它,则应该UITableViewHeaderFooterView
返回。
回答by Dag ?gren
Your header view must inherit from UITableViewHeaderFooterView
or else this call will not work and always returns nil.
您的标题视图必须继承自UITableViewHeaderFooterView
,否则此调用将不起作用并始终返回 nil。
回答by cjd
heightForRowAtIndexPath
called before viewForHeaderInSection
so of course you will get nil
.
heightForRowAtIndexPath
之前打电话viewForHeaderInSection
所以当然你会得到nil
。