简体   繁体   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;
}

Here in this code section returns 1 when I debug and hence the section 0 code doesn't get executed. 这个代码部分在我调试时返回1,因此第0部分代码没有被执行。 Thus I am not able to get Overall Progress text in my table header view. 因此,我无法在表格标题视图中获取“整体进度”文本。

The solution is to implement heightForHeaderInSection with the height that you want. 解决方案是使用您想要的高度实现heightForHeaderInSection。

   - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 44;
    }

from tableView:heightForHeaderInSection: 来自tableView:heightForHeaderInSection:

Prior to iOS 5.0, table views would automatically resize the heights of headers to 0 for sections where tableView:viewForHeaderInSection: returned a nil view. 在iOS 5.0之前,对于tableView:viewForHeaderInSection:返回nil视图的节,表视图会自动将标题的高度调整为0。 In iOS 5.0 and later, you must return the actual height for each section header in this method. 在iOS 5.0及更高版本中,您必须在此方法中返回每个节标题的实际高度。

from tableView:viewForHeaderInSection: 来自tableView:viewForHeaderInSection:

The returned object can be a UILabel or UIImageView object, as well as a custom view. 返回的对象可以是UILabel或UIImageView对象,也可以是自定义视图。 This method only works correctly when tableView:heightForHeaderInSection: is also implemented. 只有在实现tableView:heightForHeaderInSection:时,此方法才能正常工作。

see UITableViewDelegate protocol reference 请参阅UITableViewDelegate协议参考

What is in the implementation of tableView:heightForHeaderInSection:? tableView的实现是什么:heightForHeaderInSection:? If you return 0 there for section 0, then our view will be resized to be not visible at all. 如果在0处返回0,那么我们的视图将被调整为根本不可见。

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

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