TableTie 1.0.4

TableTie 1.0.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2017年4月
SwiftSwift 版本3.0
SPM支持 SPM

Vladimir Kofman 维护。



TableTie 1.0.4

TableTie

一种简单的通用方法来用数据填充 UITableView

Generic Table View Controllers by objc.io

用法

假设你有一个想要在 UITableView 中显示的歌曲和专辑列表。你的模型可以是结构体、类、枚举或仅仅是字符串。让我们以结构体作为示例:

struct Song {
    let title: String
}

struct Album {
    let name: String
    let artist: String
}

class SongCell: UITableViewCell {}
class AlbumCell: UITableViewCell {}

您需要使您的模型符合 TableTie.Row 协议。您只需实现一个方法:func configure(cell:)。在这个方法中,您将按照您的模型配置单元格。请确保使用正确的 cell: 参数类型:它是通用的,可以适用于任何 UITableViewCell 的子类。

extension Song: TableTie.Row {
    func configure(cell: SongCell) {
        cell.textLabel?.text = title
    }
}

extension Album: TableTie.Row {
    func configure(cell: AlbumCell) {
        cell.textLabel?.text = "\(name) by \(artist)"
    }
}

在您的视图中,您需要创建一个 TableTie.Adapter 实例的实例,并向它提供您的模型数组。

class MyVC: UITableViewController {  
    let tieAdapter =
      TableTie.Adapter([
          Album(name: "Paranoid", artist:"Black Sabbath"),
          Song(title: "War Pigs"),
          Song(title: "Paranoid"),
          Song(title: "Planet Caravan")])

    override func viewDidLoad() {
        tableView.delegate = tieAdapter
        tableView.dataSource = tieAdapter

        super.viewDidLoad()
    }
}

别忘了将为 tableView 设置 .delegate 和 .dataSource 为您之前创建的 TableTie.Adapter 实例。

就这样!

请继续阅读以获取更多选项...

章节

...
let tieAdapter = TableTie.Adapter([
        TableTie.Section("Album", [
            Album(name: "Paranoid", artist:"Black Sabbath"),
        ]),
        TableTie.Section("Side one", [
            Song(title: "War Pigs"),
            Song(title: "Paranoid"),
            Song(title: "Planet Caravan"),
            Song(title: "Iron Man"),
        ])])
...

行高度

extension Song: TableTie.Row {
    var reuseIdentifier: String { return "reuseMePlease" }
    var rowHeight: CGFloat { return 100.0 } // <-- HERE

    func configure(cell: YourStoryboardCell) {
        //Do what you have to do here...
    }
}

行选择

  • 选项1:使用 TableTie.SelectableRow 包装器
...
    tieAdapter.set([
        SelectableRow(Song(title:"Paranoid"), self.playSong("Paranoid")),
        SelectableRow(Song(title:"Iron Man"), self.playSong("Iron Man")),
    ])
...
  • 选项2:重写行的 didSelectRow
extension Song: TableTie.Row {
...
    func didSelectRow(of tableView: UITableView, at indexPath: IndexPath) {
        // Implement your logic here...
    }
...
}
  • 选项 3:TableTie.Adapter设置didSelect闭包
...
tieAdapter.didSelect = { row, tableView, indexPath in
    // Implement your logic here...
}
...

授权

TableTie 在 MIT 授权下可用。更多信息请参阅 LICENSE 文件。