ios - 具有一个 float 标题的 Tableview 多个部分

标签 ios swift tableview

我想实现带有部分的表格,但我希望只有一个部分应该有标题,并且对于所有部分,这个单独的标题应该显示为 float 。

有没有人有解决办法,请帮助我。

最佳答案

我同意 SuperDuperTango 在这种情况下只使用 one 部分。转换分段数据源的简单方法如下所示:

struct Section {
    let title: String
    let rows: [Row]
}

struct Row {
    let title: String
}

class TableViewController: UITableViewController {

    // original data source
    let sections: [Section] = {
        var sections = [Section]()

        for section in ["A", "B", "C", "D", "E"] {
            let numberOfRows = 1...Int.random(in: 1...5)
            let rows = numberOfRows.map { Row(title: "Section \(section), Row \($0)") }
            let section = Section(title: "Section \(section)", rows: rows)
            sections.append(section)
        }

        return sections
    }()

    // transformed data source
    var allRows: [Row] {
        return sections.reduce([], { $0 + $1.rows })
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Your section title"
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return allRows.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = allRows[indexPath.row].title
        return cell
    }

}

关于ios - 具有一个 float 标题的 Tableview 多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553258/

相关文章:

ios - Tableview 以模态方式呈现和自动布局

javascript - SproutCore:SC.TableView的替代品

iOS同时向多个蓝牙设备播放音频

swift - 如何获取符合 `Sequence` 的类型的计数?

ios - Swift 后台执行选择器

ios - 桥接 swift 和 objective-c

swift - 如何快速在动态 TableView 中创建动态 tableView?

ios - 发布 Facebook Open Graph 故事时如何选择一对多?

iphone - ios 应用程序第二屏幕

iphone - -[__ NSCFDictionary objectAtIndex:]:将JSON页面解析为UITableView时,无法识别的选择器发送到实例0x8943940