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

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

非常感谢,


当前回答

@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
        }
    }

}

其他回答

实现这一点的一个可能更简单的方法:

objective - c:

CGFloat dummyViewHeight = 40;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
self.tableView.tableHeaderView = dummyView;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);

迅速:

let dummyViewHeight = CGFloat(40)
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
self.tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: 0, right: 0)

分段标题现在将像任何常规单元格一样滚动。

裁判:https://stackoverflow.com/a/26306212

let tableView = UITableView(frame: .zero, style: .grouped)

PLUSE

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return 40
    }else{
        tableView.sectionHeaderHeight = 0
    }

    return 0
}

这有助于使用标题空间

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

以下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
}

您应该能够通过使用自定义单元格来处理标题行来伪造这一点。然后,这些单元格将像表视图中的任何其他单元格一样滚动。

您只需要在cellForRowAtIndexPath中添加一些逻辑,以便在单元格是标题行时返回正确的单元格类型。

你可能不得不自己管理你的部分,也就是说,把所有的东西都放在一个部分,并伪造标题。(你也可以尝试为头视图返回一个隐藏视图,但我不知道这是否会工作)