Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a custom initializer on a UITableViewCell?

I have a custom UITableViewCell and I'd like to use it in my table view. Here's my code for the cell:

class ReflectionCell: UITableViewCell {

@IBOutlet weak var header: UILabel!
@IBOutlet weak var content: UILabel!
@IBOutlet weak var author: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
}

init(data: Reflection) {
    self.header.text = data.title
    self.content.text = data.content
    self.author.text = data.author.name
    super.init(style: UITableViewCellStyle.default, reuseIdentifier: "reflectionCell")
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
}

I have a model class Reflection that I'd like to initialize the cell with. However, in my view controller I need to use tableView.dequeueReusableCell(withIdentifier: "reflectionCell", for: indexPath). Is there any way for me to use a custom initializer like the one I made?

like image 327
A Tyshka Avatar asked Dec 16 '17 02:12

A Tyshka


People also ask

How do I create a custom uitableviewcell?

Since we are creating a custom UITableViewCell, our subclass will be of type UITableViewCell. Once you have selected the subclass make sure to check the box next to the label that says Also create XIB file. Once you have done this, you can click next. Below is an image of what this looks like: After that we just need to save the file.

How do I override the cell style of a UITableView cell?

The init (style:reuseIdentifier) method is a great place to override the cell style property if you are going to use the default UITableViewCell programmatically, but with different styles (there is no option to set cellStyle after the cell was initialized).

How to create a table view programmatically in iOS?

How to create a table view programmatically? Let's jump straight into the coding part, but first: start Xcode, create a new iOS single view app project, enter some name & details for the project as usual, use Swift and finally open the ViewController.swift file right away. Now grab your keyboard! ⌨️

What are the advantages of using UITableView?

Through a simple delegate pattern, we can provide various information for the UITableView class, so it'll to know how much sections and rows will be needed, what kind of cells should be displayed for each row, and many more little details. Another thing is that UITableView is a really efficient class.


1 Answers

If you use dequeueReusableCell you cannot change which initializer method that is called. But you can write your own method to update the IBOutlets which you then call after you successfully dequeue a cell.

class ReflectionCell: UITableViewCell {

    @IBOutlet weak var header: UILabel!
    @IBOutlet weak var content: UILabel!
    @IBOutlet weak var author: UILabel!

    func update(for reflection: Reflection) {
        header.text = reflection.title
        content.text = reflection.content
        author.text = reflection.author.name
    }

}

And

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! ReflectionCell
    cell.update(for: reflections[indexPath.row])
    return cell
}
like image 133
Rob Avatar answered Oct 07 '22 23:10

Rob