【问题标题】:UITableView(grouped) adds extra empty header at the bottomUITableView(grouped) 在底部添加额外的空标题
【发布时间】:2015-06-16 08:21:03
【问题描述】:

我是 Objective C 和 iOS 的新手,所以我的问题可能很愚蠢:) 我在调试模式下检查了 self.tableData,那里没有空项,numberOfSectionsInTableView 返回正确的值。 下面是 UITableView 数据源方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *arr = self.tableData[section];
return arr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sortedDates.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSLog(@"%d", [tableView.dataSource tableView:tableView numberOfRowsInSection:section]);
    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
        return 0;
    } else {
        return 20.0f;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = [[UIView alloc] initWithFrame: CGRectZero];
    footer.backgroundColor = [UIColor whiteColor];
    return  footer;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section < self.sortedDates.count) {
        UILabel *sectionHeader = [[UILabel alloc] initWithFrame:[tableView rectForHeaderInSection:section]];
      //Setting header appearance
    return sectionHeader;
} else {
    UIView *blankHeader = [[UIView alloc] initWithFrame: [tableView rectForHeaderInSection:section]];
    blankHeader.backgroundColor = [UIColor whiteColor];
    return blankHeader;
    }
}

这是我得到的:

在模拟器中: http://take.ms/rm2DL

在视图层次结构中: http://take.ms/oALdi

这个空白空间正好有超过 20 磅的高度,所以我认为它应该是某种奇怪的标题。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    不要为页脚返回 CGRectZero 视图,而是简单地返回 nil。

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        return  nil;
    }
    

    另外你似乎没有实现heightForFooterInSection:section。那应该返回 0.0。

    这似乎以前出现过,iOS 似乎经常将底部填充到 20 点。 SO问题与iOS6有关,但我注意到与iOS8.3相关的评论,因此可能仍然相关。 Grouped UITableView has 20px of extra padding at the bottom

    【讨论】:

    • 谢谢你,罗里。这有帮助:-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; }
    【解决方案2】:

    您确定返回的不是页脚视图吗? 尝试在那里更改 footerForSection 方法以返回 nil

    【讨论】:

    • 试过了,结果还是一样。但是 return nil view 比我的解决方案更清楚。所以还是谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多