UITableView: custom header title view doesn't show

旧城冷巷雨未停 提交于 2019-12-24 03:23:36

问题


I want to display a table with custom header titles. The table view is attached to a controller class that implements the tableview delegate and data source protocols but is not a subclass of UIViewController because the table is a subview to be displayed above another tableview.

some snippets of my code: The tableview is created programmatically:

    _myListView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];

[_myListView setDataSource:self.myListController];
[_myListView setDelegate:self.myListController];
[_myListView setBackgroundColor:darkBackgroundColor];

where myListController is a strong property in the class.

For the number of rows in sections:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    …
    return count;    
}

The number of sections:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [someDelegate sectionCount];
}

For the custom Header View:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)];
    UILabel* sectionHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 300, 24)];

    [headerView setBackgroundColor:[UIColor clearColor]];

    sectionHeaderTitle.text = [self myTitleForHeaderInSection:section];
    sectionHeaderTitle.textColor = [UIColor whiteColor];
    sectionHeaderTitle.textAlignment = UITextAlignmentLeft;

   [headerView addSubview:sectionHeaderTitle];

    return headerView;
}

For the custom headerViewHeight (as required since iOS5):

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ( [self tableView:tableView numberOfRowsInSection:section] > 0) {
        return SectionHeaderHeight;
    } else {
        return 0;
    }
}

Sadly, the tableview does not display any section headers just as if I would return nil. However, I have checked with a breakpoint, that the code actually returns an UIView.

Everything else works fine.

What am I missing? PLease, don't hesitate to make me feel ashamed of my self.


回答1:


I don't really understand why you want to use a custom view, and not the "standard" one ? You may have your reasons, but I don't see anything in your code telling me why :)

I would personally just use this:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) return @"First section header title";
    if (section == 1) return @"Second section header title";
    else return nil;
}

Tell me if that's not what you're looking for !




回答2:


I seem to have found a solution:

I have created a lazy loading strong property for each header view I want to display. (luckily there are only three)

Now the views are shown.

It seems that the header views got deallocated without the strong references before the table was rendered.

Could it be that there is a connection to the class implementing the table view delegate and data source protocols is not a UIViewController?




回答3:


Text Color you change it to Black color and check once.

sectionHeaderTitle.textColor = [UIColor blackColor];




回答4:


you try this code this work on my side :-)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 24)];

    UIImage *myImage = [UIImage imageNamed:@"SectionBackGround.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    imageView.frame = CGRectMake(0,0,320,24);

    UIImage *imageIcon = [UIImage imageNamed:@"SectionBackGround.png"];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:myImage];
    iconView.frame = CGRectMake(0,0,320,24);

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 24)];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:14];


    [view addSubview:imageView];
    [view addSubview:iconView];
    [view addSubview:label];
    return view;

}

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


来源:https://stackoverflow.com/questions/12088257/uitableview-custom-header-title-view-doesnt-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!