【问题标题】:viewForHeaderInSection stopped working in xcode 8viewForHeaderInSection 在 xcode 8 中停止工作
【发布时间】:2016-09-22 17:29:57
【问题描述】:

我有一个包含 2 个部分的 tableView,部分数组固定有 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;
}

【问题讨论】:

  • 这不是表格视图委托或数据源方法。
  • 你的tableView:viewForHeaderInSection: 调用这个方法了吗?或者更简单,只需将此代码放入您的viewForHeaderInSection。最后,请向我们展示您的viewForHeaderInSection
  • 我把上面的代码改成了viewForHeaderInSection,还是没有被调用。顺便说一句,上面的代码在 xcode7 中被调用得很好,但不是 xcode8。
  • this 帖子上回答了类似的查询。希望这会有所帮助。

标签: ios objective-c uitableview uitableviewsectionheader


【解决方案1】:

按原样使用这个方法,你会看到标题

注意:设置tableView的delegate和datasource

- (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;
}

【讨论】:

  • 你用的是Xcode 8还是xcode 7,因为我的方法和你一样。就像我说的那样,它总是有效的,我所做的只是升级 xcode,标题就消失了。
  • XCode 8 测试版
  • 我实际上错过了 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; ........当我添加它时它工作正常......但是,以前的 Xcode(7) 不需要这个......谢谢。
  • 不客气。乐意效劳。但我认为这在 XCode 7 中也是需要的。
【解决方案2】:

确保除了 viewForHeaderInSection 函数之外还有 heightForHeaderInSection

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2017-02-12
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    相关资源
    最近更新 更多