【问题标题】:how to create dynamic tableview inside dynamic tableview in swift?如何在swift中的动态表视图中创建动态表视图?
【发布时间】:2018-09-12 07:42:50
【问题描述】:

这就是我想做的。 我想要里面的tableview,返回动态计数。

例如,第一个单元格中的 3 行,第二个单元格中的 2 行,第三个单元格中的 4 行...像这样。

tableview 之外的计数也是动态的。

我应该返回什么tableView 单元格计数?

【问题讨论】:

  • 如果您想在表格视图中进行表格视图,您应该分别处理每个表格视图委托。在单元格中,您应该订阅第二个表格视图并返回您需要的计数。
  • @biloshkurskyi.ss 您好,感谢您的回复。你能解释更多吗?我还不能理解;(你的意思是为里面的tableview创建tableview类吗?
  • 我不明白,为什么你需要这样的功能。您可以使用具有多个部分的单个 tableview 来实现相同的目标。
  • @MehulThakkar 我可以同时制作动态部分和动态单元格吗?
  • @poq :是的,为什么不呢。你可以

标签: ios swift uitableview dynamic tableview


【解决方案1】:

我认为您应该为 2 个表视图创建 2 个数据源。像这样的东西:

final class YouViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    let outerModel: [Any] = []
    lazy var outerDatasource = OuterTableDatasource(model: self.outerModel)
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = outerDatasource
        self.tableView.delegate = outerDatasource

    }
}

final class CellThatContainTableView: UITableViewCell {
    @IBOutlet weak var tableView: UITableView!
    var dataSource: InnerTableDatasource! {
        didSet {
            self.tableView.dataSource = dataSource
            self.tableView.delegate = dataSource
        }
    }
    func configure(dataSource: InnerTableDatasource) {
        self.dataSource = dataSource
    }
}

final class OuterTableDatasource: NSObject {
    var model: [Any]
    init(model: [Any]) {
        self.model = model
    }
}

extension OuterTableDatasource: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //implement cell configuration
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellThatContainTableView
        cell.configure(dataSource: InnerTableDatasource(model: []))
        return cell
    }


}
extension OuterTableDatasource: UITableViewDelegate {

}

final class InnerTableDatasource: NSObject {
    var model: [Any]
    init(model: [Any]) {
        self.model = model
    }
}

extension InnerTableDatasource: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //implement cell configuration
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!


        return cell
    }


}
extension InnerTableDatasource: UITableViewDelegate {

}

【讨论】:

    【解决方案2】:

    你可以像这样制作你的数据源:

    class TableViewDataSource {
       var outerTVDatasource: [OuterTableViewDataSource]?
    }
    
    class OuterTableViewDataSource {
       var insideTVDatasource: [InsideTableViewDataSource]?
    }
    
    class InsideTableViewDataSource {
    }
    

    在你有父表视图的主视图控制器中,返回 numberOfRows 为

    class MainViewController: UIVIewController {
      var outerTableView: UITableView?
    override func viewDidLoad() {
    super.viewDidLoad()
    outerTableView.delegate = self
    outerTableView.dataSource = self
    }
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewDataSourceObject.outerTVDatasource.count ?? 0
      }
    
    //PseudoCode
    func cellForRowAt() {
    let cell = dequeue as? OuterTableViewCell
    cell.dataSource =  tableViewDataSourceObject.outerTVDatasource
    return cell
    }
    
    }
    

    让你的 OuterTableViewCell 处理内部 tableview

    class OuterTableViewCell: UITableViewCell {
    
    var innerTableView: UITableView?
    var dataSource: OuterTableViewDataSource?
    
    //psuedo code
    awakeFromNib() {
    innerTableView.delegate = self
    innerTableView.datasource = self
    }
    
        numberOfrow() {
    return dataSource. insideTVDatasource.count
    }
    cellforrow() {
    let cell = dequeue as? InnerTableViewCell
    cell.dataSource = dataSource. insideTVDatasource
    return cell
    }
    
    
    class InnerTableViewCell: UITableViewCell {
    var dataSource: InsideTableViewDataSource
    }
    

    【讨论】:

    • 我是否必须在 tableview 的数据源内部连接并委托与外部 tableview 相同的 viewcontroller?
    • 我应该在哪里创建数据源?我必须创建另一个类吗?以及如何将它与内部 tableview 连接??
    • 谢谢你,但有些东西我听不懂。什么是 tableViewDataSourceObject?
    【解决方案3】:

    查看这篇博文了解详情https://medium.com/@stasost/ios-how-to-build-a-table-view-with-multiple-cell-types-2df91a206429

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let item = items[indexPath.section]
       switch item.type {
       case .nameAndPicture:
          if let cell = tableView.dequeueReusableCell(withIdentifier: NamePictureCell.identifier, for: indexPath) as? NamePictureCell {
             cell.item = item
             return cell
          }
       case .about:
          if let cell = tableView.dequeueReusableCell(withIdentifier: AboutCell.identifier, for: indexPath) as? AboutCell {
             cell.item = item
             return cell
          }
       case .email:
          if let cell = tableView.dequeueReusableCell(withIdentifier: EmailCell.identifier, for: indexPath) as? EmailCell {
             cell.item = item
             return cell
          }
       case .friend:
          if let cell = tableView.dequeueReusableCell(withIdentifier: FriendCell.identifier, for: indexPath) as? FriendCell {
             cell.item = friends[indexPath.row]
             return cell
          }
       case .attribute:
          if let cell = tableView.dequeueReusableCell(withIdentifier: AttributeCell.identifier, for: indexPath) as? AttributeCell {
             cell.item = attributes[indexPath.row]
             return cell
          }
       }
       // return the default cell if none of above succeed
       return UITableViewCell()
    }
    
    You can use the same structure to setup the didSelectRowAt delegate method:
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          switch items[indexPath.section].type {
              // do appropriate action for each type
          }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多