viewForHeaderInSection在xcode 8中停止工作

use*_*412 -2 objective-c uitableview ios uitableviewsectionheader

我有一个包含2个部分的tableView,sections数组用2个元素固定.这些部分的标题显示正常,因为下面的代码显示在Xcode 7中.我刚刚升级到Xcode 8,并且部分标题不再显示,下面的代码不再被调用.

有任何想法吗?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    NSString *date =[sections objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:date];
    [view addSubview:label];
    [view setBackgroundColor:[DeviceHelper Orange]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}
Run Code Online (Sandbox Code Playgroud)

Jan*_*aya 7

按原样使用此方法,您将看到标题

注意:设置tableView的委托和数据源

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *reuseId = @"reuseid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }

    cell.textLabel.text = @"Test";

    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    NSString *date = @"22nd Sept 2016";
    /* Section header is in 0th index... */
    [label setText:date];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor orangeColor]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/25

    return view;
}
Run Code Online (Sandbox Code Playgroud)

  • 我实际上是缺少 - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {return 40; } ........当我添加它时它工作得很好......但是,在以前的Xcode(7)中不需要这个...谢谢. (3认同)