首页 > 解决方案 > 在 Tableview 中对单元格进行分组、在单元格上方或下方添加文本以及空白区域

问题描述

我正在尝试实现类似于页面底部的图像。具体来说:

  1. 在每个单元格之间添加间距(您可以说在不同的组中。)
  2. 在单元格上方或下方添加文本。
  3. 其他地方都有空的空间,没有细胞。

我希望一切看起来都像这张图片。 (忽略文本、导航栏和单元格内的任何内容。)

我已经尝试过了,并得到了下面这张图片:

我已经尝试过的例子

这是我尝试过的代码:

let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"]]
let headerTitles = ["Some Data 1", "KickAss"]

    // MARK: - TableView Data Source Methods

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let cellText = data[indexPath.section][indexPath.row]

        cell.textLabel?.text = cellText

        return cell
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section < headerTitles.count {
            return headerTitles[section]
        }

        return nil
    }

我想要实现的示例。

标签: iosswiftuitableview

解决方案


TableView 状态本文档将帮助您了解 tableview 状态。(加载、数据、空状态)


推荐阅读