我使用UITableView来布局内容“页面”。我使用表视图的标题来布局某些图像等,我更喜欢它,如果他们没有浮动,但保持静态,因为他们做的时候,风格设置为UITableViewStyleGrouped。

除了使用UITableViewStyleGrouped,有办法做到这一点吗?我想避免使用分组,因为它增加了我所有的单元格的边缘,并要求为每个单元格禁用背景视图。我想完全控制我的布局。理想情况下,它们应该是“UITableViewStyleBareBones”,但我在文档中没有看到这个选项…

非常感谢,


当前回答

While thinking how to approach this problem, I remembered a very important detail about UITableViewStyleGrouped. The way UITableView implements the grouped style (the rounded borders around the cells) is by adding a custom backgroundView to the UITableViewCells, and not to the UITableView. Each cell is added a backgroundView according to its position in the section (upper rows get the upper part of the section border, middle ones get the side border and the bottom one gets – well, the bottom part). So, if we just want a plain style, and we don’t have a custom backgroundView for our cells (which is the case in 90% of the times), then all we need to do is use UITableViewStyleGrouped, and remove the custom background. This can be done by following those two steps:

将我们的tableView样式改为UITableViewStyleGrouped 在cellForRow返回单元格之前,添加以下一行:

细胞。开源视图=[[UIView alloc]这是一个细胞:跳跃]autorelease];

就是这样。tableView样式将变得完全像UITableViewStylePlain,除了浮动头。

希望这能有所帮助!

其他回答

警告:这个解决方案实现了一个保留的API方法。这可能会阻止苹果批准该应用程序在AppStore上发布。

我已经在我的博客中描述了使节头浮动的私有方法

基本上,你只需要子类化UITableView并在它的两个方法中返回NO:

- (BOOL)allowsHeaderViewsToFloat;
- (BOOL)allowsFooterViewsToFloat;

Swift 3+

简单地使用UITableViewStyleGrouped并将页脚的高度设置为0,如下所示:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}

获得你想要的最简单的方法是将你的表格样式设置为UITableViewStyleGrouped, separator样式为UITableViewCellSeparatorStyleNone:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN; // return 0.01f; would work same 
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

不要尝试返回页脚视图为nil,不要忘记设置页眉高度和页眉视图,之后你必须得到你想要的。

忽略XAK。如果你想让你的应用有机会被苹果接受,就不要探索任何私有方法。

如果您正在使用Interface Builder,这是最简单的。你可以在视图的顶部(图像所在的位置)添加一个UIView,然后在下面添加你的tableview。IB应相应地调整其大小;也就是说,tableview的顶部接触到你刚刚添加的UIView的底部,它的高度覆盖了屏幕的其余部分。

这里的想法是,如果UIView不是表格视图的一部分,它不会随着tableview滚动。即忽略tableview头。

如果你不使用界面构建器,它会更复杂一些因为你必须为tableview获得正确的定位和高度。

还有另一种棘手的方法。主要思想是将节数加倍,第一个只显示headerView,而第二个显示真正的单元格。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return sectionCount * 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section%2 == 0) {
        return 0;
    }
    return _rowCount;
}

接下来需要做的是实现headerInSection委托:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section%2 == 0) {
        //return headerview;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section%2 == 0) {
        //return headerheight;
    }
    return 0;
}

这种方法对你的数据源也没有什么影响:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int real_section = (int)indexPath.section / 2;
    //your code
}

与其他方法相比,这种方法是安全的,同时不改变tableview的框架或contentInsets。 希望这能有所帮助。