我必须将一组动态数据组织为一组滑动表。现在我正在使用 UIPageViewController 来完成这项任务,但它在从网络动态上传数据方面存在一些问题 - 如果我们滑动页面速度太快,我们可能会超过数据加载并且程序可能会崩溃。现在,为了解决这个问题,我提前 5 页上传数据,但我认为这是不好的解决方案,我希望这个任务有一个正确的解决方案。 我发现了另一个想法 - 使用 UICollectionView 完成此任务,但我不确定我是否可以在此方法中将表用作 UICollectionViewCell,而且我不确定这个决定是否正确。 在这种情况下你能推荐什么
最佳答案
这是我处理这种情况的方法
- 首先,我会让我的 ViewController 包含如下所示的 collectionView - 确保添加您自己的约束,但是您愿意 -
import UIKit
class ViewController: UIViewController {
// IBOutlets
@IBOutlet var collectionView: UICollectionView!
// MARK: Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
// MARK: Private Methods
private func setupCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
collectionView.separatorStyle = .none
collectionView?.register(UINib(nibName: /* Your cell NibName */, bundle: nil), forCellWithReuseIdentifier: /* Your cell Id*/)
collectionView?.isPagingEnabled = true
}
// MARK: UICollectionView Methods
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return zekr?.subNodes?.count ?? 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = zekrCollectionView.dequeueReusableCell(withReuseIdentifier: /* Your cell Id */, for: indexPath) as! /* Your cell Type */
cell.cellData = /* your list that corresponds with the list that table will take */
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// this is to make each cell fill the whole page
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
- 然后您将在每个 collectionViewCell 中添加 tableview,并确保为 collectionViewCell 中的 tableView 实现委托(delegate)和数据源,如下所示
import UIKit
class CollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {
// MARK: IBOutlets
@IBOutlet weak var pageTable: UITableView!
var cellData: [/*Your List*/]?
// MARK: Properties
var cellData: /*Your List*/?{
didSet{
if let value = cellData {
/* reload your table */
}
}
}
// MARK: Life Cycle Methods
override func awakeFromNib() {
super.awakeFromNib()
setupPageTable()
}
private func setupPageTable(){
pageTable.delegate = self
pageTable.dataSource = self
pageTable.register(UINib(nibName: /* TableView Cell NibName */, bundle: nil), forCellReuseIdentifier: /* CellId */)
}
// MARK: UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellData?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: /* CellId */) as! /* Cell Type */
cell.cellDataModel = cellData?[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cell: UITableViewCell = cellData?[indexPath.row]
return cell.contentView.frame.size.height
}
}
最后,您的 tableViewCell 将保持不变,但您最初将 cellDataModel 传递给它是这样做的
如果水平滚动对 collectionView 不起作用,您可以根据您的 swift 版本在 google 上搜索解决方案
结果是,您将在家中或任何 VC 上拥有 collectionView,水平滚动每个包含 tableView 的单元格,其垂直滚动的单元格充当 android 中的 viewPager
关于ios - 如何快速组织uitableview的动态数量作为滑动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56921071/