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

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

非常感谢,


当前回答

还有另一种棘手的方法。主要思想是将节数加倍,第一个只显示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。 希望这能有所帮助。

其他回答

也许你可以在tableView上使用scrollViewDidScroll 并根据当前可见头更改contentInset。

它似乎适用于我的用例!

extension ViewController : UIScrollViewDelegate{

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard   let tableView = scrollView as? UITableView,
            let visible = tableView.indexPathsForVisibleRows,
            let first = visible.first else {
            return
        }

        let headerHeight = tableView.rectForHeader(inSection: first.section).size.height
        let offset =  max(min(0, -tableView.contentOffset.y), -headerHeight)
        self.tableView.contentInset = UIEdgeInsetsMake(offset, 0, -offset, 0)
   }
}

你可以通过在tableview delegate类中实现viewForHeaderInSection方法轻松实现。这个方法期望一个UIView作为返回对象(这是你的头视图)。我在代码中也做了同样的事情

我有另一个更简单的解决方案,使用没有自动布局和一切通过XIB完成:

1/把你的头放在tableview中,直接拖放到tableview上。

2/在新建头部的尺寸检查器中,改变它的自动大小:你应该只保留顶部,左右锚,加上水平填充。

这应该能奏效!

一个棘手的方法是为标题添加一个空部分。因为section没有单元格,所以它根本不会浮动。

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