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

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

非常感谢,


当前回答

以下2种场景的最佳解决方案:

场景1:如果tableView样式不是.grouped。改成分组没问题。页脚不会浮动。

场景2:如果你只想在tableView的末尾有一个页脚。它的风格是朴素的

步骤:

最后再加一节。 确保在添加的部分有(nuberOfRowsInSection方法)零行数。 向该节添加自定义页脚。 将highightforfooterinsection设置为自定义页脚,将. 0设置为其他部分的页脚。

例子:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

//注意:过滤你想要添加自定义section的section

    let footerView = UIView()
    submitOrganiser?.showFooterAtTheEnd(view: footerView) //my method to customise the footer, use your implementation
    return footerView
}

其他回答

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

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

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

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

检查如何用StoryBoard实现标题:在StoryBoard中的表标题视图

还要注意,如果您没有实现

viewForHeaderInSection:(NSInteger)section

它不会浮动,这正是你想要的。

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,除了浮动头。

希望这能有所帮助!

另一种方法是在你想要放置标题的区域之前创建一个空区域,然后把你的标题放在这个区域上。因为部分是空的,所以标题会立即滚动。

@samvermette的解决方案的变体:

/// Allows for disabling scrolling headers in plain-styled tableviews
extension UITableView {

    static let shouldScrollSectionHeadersDummyViewHeight = CGFloat(40)

    var shouldScrollSectionHeaders: Bool {
        set {
            if newValue {
                tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: UITableView.shouldScrollSectionHeadersDummyViewHeight))
                contentInset = UIEdgeInsets(top: -UITableView.shouldScrollSectionHeadersDummyViewHeight, left: 0, bottom: 0, right: 0)
            } else {
                tableHeaderView = nil
                contentInset = .zero
            }
        }

        get {
            return tableHeaderView != nil && contentInset.top == UITableView.shouldScrollSectionHeadersDummyViewHeight
        }
    }

}