Use Prototype Cell From Another View Controller

心不动则不痛 提交于 2019-12-12 10:37:56

问题


I want to use the same table view cell layout in several storyboard scenes. Can I design the prototype cell in one of the scenes and somehow access it (i.e. dequeueReusableCellWithIdentifier) in another table view controller?


回答1:


This is not possible, but you can copy the prototype cell from source table view to destination one inside storyboard and you can easily reuse it.




回答2:


You can design your prototype cell in a .xib file and import that into multiple UITableViewController subclasses. Just make sure to keep the identifier in synch between your references in code and your prototype cell.

class YourViewController: UITableViewController {

    func viewDidLoad() {
        super.viewDidLoad()

        let nib = UINib(nibName: "your_file_name", bundle: nil)
        tableView.registerNib(nib, forCellWithReuseIdentifier: "your_cell_identifier")
        // ... 
    }

}

Same applies to custom UICollectionViewCell prototypes and their use in UICollectionView subclasses.



来源:https://stackoverflow.com/questions/25921924/use-prototype-cell-from-another-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!