Is it possible to create one tableViewCell that can be used in multiple table controllers?

I have about 3 or 4 table controllers that will use the same tableview cell. I keep returning to the fourth possible logic. Is it possible to use the same tableViewCell in several TableView controllers if the information between them is the same? Or do I need to create a new cell for each controller?

0
source share
2 answers

Yes, you can.

I assume you are using Swift.

Goto File -> New and select the cocoaTouch class as follows.

enter image description here

UITableViewCell. " Xib"

enter image description here

Xib .Swift. , tableView, .

enter image description here

ImageView anyThing, .

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

return cell
}

tableView ,

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
// do something with your cell

}

, .

Swift 3 Swift 4:

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell

    return cell
}
+3

, TableView.

0

Source: https://habr.com/ru/post/1666599/


All Articles