How to Add Images to UITableView Cells in UIKit

Written by

in

Quick answer: Use cell.imageView?.image = UIImage(named: "name") for default-style cells. For custom layouts, add a UIImageView to the cell’s contentView with Auto Layout constraints, and configure it in cellForRowAt or a custom cell subclass.

Why this happens

UITableView cells don’t automatically display images unless you explicitly assign them. The built-in imageView property only exists on cells created with the default style (UITableViewCell.CellStyle.default). If you use a different style — such as .subtitle, .value1, or .value2 — or a custom cell class, imageView is nil and any assignment is silently ignored.

Another common issue is cell reuse. A dequeued cell may still hold an image from a previous row. If you don’t reset or replace the image for every row, stale images appear while scrolling.

How to fix it

1. Use the built-in imageView for default-style cells

This is the simplest approach and works when you initialize the cell with .default style (or don’t specify a style at all).

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.imageView?.image = UIImage(named: "icon-star")
    cell.textLabel?.text = items[indexPath.row]
    return cell
}

Important: Register the cell with the default style:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// or in a storyboard, set Style to "Basic" in the Attributes inspector

If you need a different style, this property won’t exist — move to the next approach.

2. Add a UIImageView to the contentView with Auto Layout (recommended)

For any custom layout, create a UIImageView, add it to contentView, and constrain it. Do this once — ideally in a custom UITableViewCell subclass — so you don’t rebuild the view hierarchy on every dequeue.

final class ImageCell: UITableViewCell {
    static let reuseID = "ImageCell"
    private let iconView = UIImageView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }
    required init?(coder: NSCoder) { fatalError() }

    private func setup() {
        iconView.translatesAutoresizingMaskIntoConstraints = false
        iconView.contentMode = .scaleAspectFit
        contentView.addSubview(iconView)

        NSLayoutConstraint.activate([
            iconView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
            iconView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            iconView.widthAnchor.constraint(equalToConstant: 28),
            iconView.heightAnchor.constraint(equalToConstant: 28),
            // give the text label room if you also use textLabel
            textLabel?.leadingAnchor.constraint(equalTo: iconView.trailingAnchor, constant: 12) ?? NSLayoutConstraint()
        ])
    }

    func configure(with imageName: String, title: String) {
        iconView.image = UIImage(named: imageName)
        textLabel?.text = title
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        iconView.image = nil
        textLabel?.text = nil
    }
}

Register and use it:

tableView.register(ImageCell.self, forCellReuseIdentifier: ImageCell.reuseID)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ImageCell.reuseID, for: indexPath) as! ImageCell
    cell.configure(with: "icon-star", title: items[indexPath.row])
    return cell
}

3. Quick one-off without a subclass (contentView subview)

If you only need an image in one place and don’t want a subclass, add the image view in cellForRowAt but guard against duplicate subviews on reuse:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    // Reuse or create the image view
    let tag = 999
    let imageView: UIImageView
    if let existing = cell.contentView.viewWithTag(tag) as? UIImageView {
        imageView = existing
    } else {
        imageView = UIImageView()
        imageView.tag = tag
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFit
        cell.contentView.addSubview(imageView)
        NSLayoutConstraint.activate([
            imageView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
            imageView.centerYAnchor.constraint(equalTo: cell.contentView.centerYAnchor),
            imageView.widthAnchor.constraint(equalToConstant: 24),
            imageView.heightAnchor.constraint(equalToConstant: 24)
        ])
    }
    imageView.image = UIImage(named: "icon-star")
    cell.textLabel?.text = items[indexPath.row]
    return cell
}

This works but mixes view construction with data binding — prefer the subclass for anything reused across the app.

4. Use a configuration-based cell (iOS 14+)

UIListContentConfiguration lets you set an image and text without touching imageView directly. It works with the default cell and respects the cell’s style.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    var config = cell.defaultContentConfiguration()
    config.text = items[indexPath.row]
    config.image = UIImage(named: "icon-star")
    config.imageProperties.maximumSize = CGSize(width: 28, height: 28)
    config.imageProperties.cornerRadius = 4
    cell.contentConfiguration = config
    return cell
}

This is the cleanest API for simple cells and automatically handles dark mode, accessibility, and reuse.

How to prevent it

  • Always reset images in prepareForReuse when using custom image views.
  • Register cells with the correct style — .default if you rely on imageView.
  • Use UIListContentConfiguration for standard image + text layouts; it avoids manual view management.
  • Constrain to contentView.layoutMarginsGuide so images respect safe areas and readable content guides.
  • Set contentMode (usually .scaleAspectFit) so artwork doesn’t stretch.

FAQ

Why is my cell’s imageView nil?

imageView only exists on cells created with UITableViewCell.CellStyle.default. If you initialized the cell with .subtitle, .value1, .value2, or a custom subclass that doesn’t call super.init(style: .default, ...), the property stays nil.

How do I change the image size without stretching?

Set imageView.contentMode = .scaleAspectFit (or .scaleAspectFill with clipsToBounds = true) and add explicit width/height constraints on the image view. With UIListContentConfiguration, use imageProperties.maximumSize.

Can I use SF Symbols in table view cells?

Yes. UIImage(systemName: "star.fill") works everywhere UIImage works — in imageView, a custom UIImageView, or contentConfiguration.image. Apply a UIImage.SymbolConfiguration for weight/scale if needed.{

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.