测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年4月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Vladimir Kofman 维护。
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"),
])])
...
TableTie
将自动为您注册 UITableViewCell
的重用。如果您需要使用特定的 reuseIdentifier
,或者如果您已经在 Storyboard 中设计了单元格,只需覆盖 TableTie.Row
的 reuseIdentifier
属性即可。
extension Song: TableTie.Row {
var reuseIdentifier: String { return "reuseMePlease" } // <-- HERE
func configure(cell: YourStoryboardCell) {
//Do what you have to do here...
}
}
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...
}
}
TableTie.SelectableRow
包装器...
tieAdapter.set([
SelectableRow(Song(title:"Paranoid"), self.playSong("Paranoid")),
SelectableRow(Song(title:"Iron Man"), self.playSong("Iron Man")),
])
...
didSelectRow
extension Song: TableTie.Row {
...
func didSelectRow(of tableView: UITableView, at indexPath: IndexPath) {
// Implement your logic here...
}
...
}
TableTie.Adapter
设置didSelect
闭包...
tieAdapter.didSelect = { row, tableView, indexPath in
// Implement your logic here...
}
...
TableTie 在 MIT 授权下可用。更多信息请参阅 LICENSE 文件。