tableView viewForHeaderInSection 从 ios7 中的第 1 节开始
Posted
技术标签:
【中文标题】tableView viewForHeaderInSection 从 ios7 中的第 1 节开始【英文标题】:tableView viewForHeaderInSection starts with section 1 in ios7 【发布时间】:2013-11-13 11:57:22 【问题描述】:- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
NSString *sectionTitle = @"";
if(section == 0)
sectionTitle = @"Overall Progress";
else
sectionTitle = [[courses objectAtIndex:section-1] objectForKey:@"course-name"];
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
label.frame = CGRectMake(15, 10, 300, 30);
label.font = [UIFont boldSystemFontOfSize:14];
else
label.frame = CGRectMake(50, 20, 600, 60);
label.font = [UIFont boldSystemFontOfSize:19];
label.text = sectionTitle;
label.backgroundColor = [UIColor clearColor];
[label sizeToFit];
// Create header view and add label as a subview
UIView *view;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
else
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 500)];
view.backgroundColor = [UIColor clearColor];
[view addSubview:label];
[view autorelease];
return view;
在此代码部分中,当我调试时返回 1,因此第 0 部分的代码不会被执行。因此,我无法在表格标题视图中获得总体进度文本。
【问题讨论】:
虽然它在 ios 6 及更低版本中运行良好 你遇到什么样的错误? 我没有遇到任何错误。好像它不是从第 0 节开始的,所以我的第 0 节代码没有被执行.... 请同时发布您的其他代表方法 第 0 部分有多少行? 【参考方案1】:解决方案是使用你想要的高度来实现 heightForHeaderInSection。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 44;
来自 tableView:heightForHeaderInSection:
在 iOS 5.0 之前,对于 tableView:viewForHeaderInSection: 返回 nil 视图的部分,表视图会自动将标题的高度调整为 0。在 iOS 5.0 及更高版本中,您必须在此方法中返回每个节标题的实际高度。
来自 tableView:viewForHeaderInSection:
返回的对象可以是 UILabel 或 UIImageView 对象,也可以是自定义视图。此方法只有在同时实现 tableView:heightForHeaderInSection: 时才能正常工作。
见UITableViewDelegate protocol reference
【讨论】:
【参考方案2】:tableView:heightForHeaderInSection: 的实现是什么?如果您在第 0 部分返回 0,那么我们的视图将被调整为根本不可见。
【讨论】:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath return 27.0;以上是关于tableView viewForHeaderInSection 从 ios7 中的第 1 节开始的主要内容,如果未能解决你的问题,请参考以下文章
tableView(cellForRowAtIndexPath) 与 tableView.cellForRowAtIndexPath()?
TableView里面的tableview cell swift 3
将 tapGesture 添加到 tableView 则无法执行 tableView(_ tableView: UITableView, didSelectRowAt indexPath: Index