Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Prototype Cell From Another View Controller

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?

like image 550
MarkF Avatar asked Sep 18 '14 20:09

MarkF


2 Answers

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.

like image 195
Fury Avatar answered Oct 25 '22 00:10

Fury


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.

like image 38
Nick Podratz Avatar answered Oct 25 '22 01:10

Nick Podratz