ios - 一个 tableView 有两个不同的 cell.xib,如果我按下一个单元格,它需要显示第二个单元格。如何?

标签 ios swift uitableview

我使用 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]


        }


    }

告诉我该怎么做,如果我按第一个单元格,它应该根据 indexPath.row 显示第二个单元格值

最佳答案

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

您可能想阅读 this :

希望这对您有所帮助!

关于ios - 一个 tableView 有两个不同的 cell.xib,如果我按下一个单元格,它需要显示第二个单元格。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619946/

相关文章:

ios - UITableView 由于在滚动 tableview 的同时重新加载数据而崩溃

ios - 如何删除第一个单元格顶部分隔符和最后一个单元格底部分隔符

ios - 如何快速隐藏静态 UItableview 中的特定单元格?

ios - 弹出 UIAlertView 时更改 UILabel 文本颜色

ios - Azure 通知中心 : notifications not showing up if sent to group

ios - 每天在设定的时间用 swift 重复本地通知

ios - Swift 中的时间变量

ios - Swift:父类类函数是否有可能返回子类的实例?

在结构中存储对象时出现 Swift 问题

ios - 在 Swift 上计算我的位置和 MapKit 引脚之间的距离