【发布时间】:2020-03-23 02:46:12
【问题描述】:
我目前在使用 UITableViewDiffableDataSource 时遇到问题。
我想尝试一下这个新功能,所以我在网上看了很多教程,但似乎没有一个能回答我的问题。
在我当前的 viewController 中,我有一个 UITableView,有 3 个不同的对象(每个对象都有不同的类型),但 UITableViewDiffableDataSource 被强类型化为一个。
点赞:dataSource = UITableViewDiffableDataSource <SectionType, ItemType>
我的所有部分都包含类似
的内容func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return bigObject.ObjectsOfType1.count
} else if section == 1 {
return bigObject.ObjectsOfType2.count
} else {
return bigObject.ObjectsOfType3.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CustomTableViewCell
if indexPath.section == 0 {
cell.buildWithFirstObject(obj: bigObject.ObjectsOfType1[indexPath.row])
} else if indexPath.section == 1 {
cell.buildWithFirstObject(obj: bigObject.ObjectsOfType2[indexPath.row])
} else {
cell.buildWithFirstObject(obj: bigObject.ObjecstOfType3[indexPath.row])
}
}
在我的情况下使用 diffable dataSource 有技巧吗?
任何帮助表示赞赏!感谢您阅读我:)
【问题讨论】:
-
这段代码遇到了什么麻烦?
-
使用 diffable 数据源似乎只允许我为一个部分加载数据,因为每个部分的“项目类型”都不同。
标签: ios swift diffabledatasource