Can I use a single prototype cell in multiple tableViews?

后端 未结 5 1935

I am having two different tableviews in two different controller. But the cells, that I need to display in them, look identical. I have created a prototype cell in one tableView

5条回答
  •  心在旅途
    2021-02-05 06:52

    Yes. We can use a prototype cell of ViewControllerA's tableview for tableview of ViewControllerB. Just need to implement the following code in ViewControllerB

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let vc_array = self.navigationController?.viewControllers 
        let view_controller = vc_array![vc_array!.count - 2] as! ViewControllerA
        let cell = view_controller.tableView.dequeueReusableCell(withIdentifier: "ViewControllerA_ID", for: indexPath) as! ViewControllerA
        
        return cell
    }
    

    Here ViewControllerA is the ViewController which contains the tableview with prototype cell, and we are using the same cell on the tableview of ViewControllerB

提交回复
热议问题