Custom Cells

suggest change

Customizing a UITableViewCell can allow for very powerful, dynamic, and responsive interfaces. With extensive customization and in combination with other techniques you can do things like: update specific properties or interface elements as they change, animate or draw things in the cell, efficiently load video as the user scrolls, or even display pictures as they download from a network. The possibilities here are nearly endless. Below is a simple example of what a custom cell may look like.

This section covers the basics, and hopefully will be expanded to detail more complex processes like those described above.

Creating Your Custom Cell

First, create a new subclass of UITableViewCell (create a new Cocoa Touch Class in Xcode and set UITableViewCell as the superclass). Below is what your code may look like after subclassing.

NSStringFromClass includes the module name, so it does not match a nib named CustomTableViewCell. Use a stable reuse identifier. awakeFromNib does not run for a cell registered by class, so create subviews in init(style:reuseIdentifier:).

Swift

class CustomTableViewCell: UITableViewCell {
    static let identifier = "CustomTableViewCell"

    var customLabel: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        customLabel = UILabel(frame: contentView.bounds)
        customLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        customLabel.textAlignment = .center
        contentView.addSubview(customLabel)
    }

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

Optionally, check “Also create a XIB file” and lay the cell out in Interface Builder. Connect customLabel as an @IBOutlet and do not create a second label in code.

In a UIViewController containing the tableView, register the new custom cell’s class (see below). Note, this is only necessary if you do not design the cell with a Storyboard in your table view’s interface.

Swift

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Register Cell Class
    tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: CustomTableViewCell.identifier)
}

If you chose to use a XIB file, registerNib instead:

Swift

// Register Nib
tableView.register(UINib(nibName: CustomTableViewCell.identifier, bundle: nil), forCellReuseIdentifier: CustomTableViewCell.identifier)

Now that your tableView knows about your custom cell, you can dequeue it in cellForRowAtIndexPath:

Swift

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // The identifier must match the one used at registration.
    let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier, for: indexPath) as! CustomTableViewCell

    // This is where the magic happens - setting a custom property on your very own cell
    cell.customLabel.text = "My Custom Cell"

    return cell
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents