如何在swift的动态tableview中创建动态tableview?

时间:2018-09-12 07:42:50

标签: ios swift uitableview dynamic tableview

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

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

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

我应该返回tableView个单元格计数吗?

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以这样创建数据源:

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
}

答案 1 :(得分:0)

我认为您应该为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 :(得分:0)

查看此博客文章以获取详细信息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
      }
}
相关问题