繁体   English   中英

tableView viewForHeaderInSection从ios7中的第1节开始

[英]tableView viewForHeaderInSection starts with section 1 in ios7

- (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部分代码没有被执行。 因此,我无法在表格标题视图中获取“整体进度”文本。

解决方案是使用您想要的高度实现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协议参考

tableView的实现是什么:heightForHeaderInSection:? 如果在0处返回0,那么我们的视图将被调整为根本不可见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM