UITableViewCell滚动自定义UITableView标题标题

时间:2015-12-22 13:53:26

标签: ios objective-c uitableview

我有一个简单的UITableViewController,其中包含大约40个UITableViewCells,而UITableViewController嵌入在UITabBarController中。在Storyboard中创建了UITableViewController

我正在覆盖自定义UITableViewHeader文字,以便我可以为用户提供一些指南。我注意到一些非常奇怪的行为,如下图所示:

enter image description here

enter image description here

可以看出,UITableView滚动,但标题文字仍然存在,看起来很糟糕。

以下是我设置自定义标题的方法:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // Custom label for the section header titles
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;

    [label setFont:[UIFont systemFontOfSize:17.0]];

    if (section == 0)
    {
        label.text = @" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
    }    
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    CGFloat headerHeight = 0;

    headerHeight = 110;

    return headerHeight;

}

我使用过不同尺寸,但无论我做什么,它仍然以同样的方式运作。

如果有人对此有任何想法或指导,那将非常感激。我只是希望标题文本也向上滚动表格视图,因此滚动时隐藏视图。

2 个答案:

答案 0 :(得分:1)

 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Custom label for the section header titles
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 110)];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;

[label setFont:[UIFont systemFontOfSize:17.0]];

if (section == 0) {
    label.text = @" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
}    
[view addSubView:label];
[view setBackgroundColor:[UIColor blueColor]]; // the color for background
return view;}

答案 1 :(得分:1)

您可以将标题添加为tableHeaderView,如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, self.tableView.frame.size.width, 110)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.numberOfLines = 0;
    [label setFont:[UIFont systemFontOfSize:17.0]];
    label.text = @" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
    self.tableView.tableHeaderView = label;
}