【问题标题】:One tableView With two different cell.xib ,If i press one cell it need to show second cell. How?一个 tableView 有两个不同的 cell.xib ,如果我按下一个单元格,它需要显示第二个单元格。如何?
【发布时间】:2019-10-30 06:48:40
【问题描述】:

我使用 tableView 和两个不同的 cell.xib 文件,我想在单击 cell1 时显示,然后我应该显示 cell2 数据。

class TableView: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var array1 = ["Click1","Click2"]
    var array2 = [[ "one","two","Three"],["Four","Five"]]
    var selectedArray = [String]()

        override func viewDidLoad() {
            super.viewDidLoad()

            tableView.register(UINib(nibName: "MainCell", bundle: nil) , forCellReuseIdentifier: "MainCell")//This is used to add xib file with identifier

            tableView.register(UINib(nibName: "SecondCell", bundle: nil) , forCellReuseIdentifier: "SecondCell")

        }

        //MARK:DataSource Methods
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array1.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = self.tableView.dequeueReusableCell(withIdentifier: "MainCell") as! MainCell
            cell.textLabel?.text = array1[indexPath.row]
            return cell
        }
        //MARK: tableViewDelegate Method
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            let cell = self.tableView.dequeueReusableCell(withIdentifier: "SecondCell") as! SecondCell

            selectedArray = array2[indexPath.row]
            cell.textLabel?.text = selectedArray[indexPath.row]


        }


    }

告诉我怎么做

【问题讨论】:

  • 如果用户按下cell2然后呢?
  • 你为什么在 numberOfRowsInSection 中返回 1?
  • 决定你是否需要change data or cell?didSelectRowAt 你不能dequeue a cell。更改数据或单击重新加载tableview 并使用boolean 检查将dequeue your second cell
  • 当我点击 click2 时,我需要在同一个 tableView 上显示四个、五个。
  • 你的帖子太混乱了。你想创建一个带有可折叠/展开部分的表格视图,对吧?参考:medium.com/@legonaftik/…

标签: ios swift uitableview


【解决方案1】:

我认为您想要实现可扩展的单元格。您可以为此使用标题单元格。

您可能想阅读this

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    例如,您可以添加一个标志来指示第一个单元格是否被点击。

    var wasFirstCellTapped = false
    

    那么numberOfRowsInSection依赖于这个标志:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return wasFirstCellTapped ? array1.count : 1
    }
    

    didSelectRowAt:indexPath 中将此标志设置为true。并执行 tableView 更改。最简单的方法是拨打tableView.reloadData()。但是您可以使用 insertRowsAtIndexPaths 对此进行动画处理

    【讨论】:

      【解决方案3】:
      var allCellsArray = ["Click1","Click2"]
      var displayingCellsArray = ["Click1"]
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return displayingCellsArray.count
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              var cell: UITableViewCell!
              if displayingCellsArray[indexPath.row] == "Click1" {
                  cell = self.tableView.dequeueReusableCell(withIdentifier: "MainCell") as! MainCell
              }else {
                  cell = self.tableView.dequeueReusableCell(withIdentifier: "SecondCell") as! SecondCell
              }
              cell.textLabel?.text = displayingCellsArray[indexPath.row]
              return cell
          }
          //MARK: tableViewDelegate Method
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
              let data = displayingCellsArray[indexPath.row]
              //Below logic will show (Cell2 if Cell1 is clicked and hide Cell1)
              // (and show Cell1 if clicked Cell2 and hide Cell2)
              if data == "Click1" {
                  displayingCellsArray = [allCellsArray.last!]            
              }else {
                  displayingCellsArray = [allCellsArray.first!]
              }
      
              tableView.reloadData()
          }
      

      您应该在单独的数据源数组中处理此类逻辑,方法是从该数组中添加/删除显示特定单元格的那种数据。 例如 在您的情况下,您在 array Click1Click2 中有两个值 Click1 显示MainCell & Click2 显示SecondCell 所以首先在你的数组中添加Click1,当点击这个单元格时,只需在你的数组中添加Click2reload。如果您想在显示SecondCell 时删除MainCell,那么在添加Click2 时只需删除Click1

      【讨论】:

      • 这里我有 2 个不同的 cell.xib,如果我点击 click1,一个和两个应该显示在 tableView 中的 click1 数组下方。
      【解决方案4】:

      您可以在单击第一个单元格时展开第二个单元格..

      参考这个例子展开和折叠单元格

      https://github.com/jonasman/JNExpandableTableView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        相关资源
        最近更新 更多