Home > Net >  UIImageView within UITableViewCell not changing image
UIImageView within UITableViewCell not changing image

Time:11-19

I'm a bit stuck. I have a custom UIView which contains a timer and an UIImageView, that timer fires at a constant interval and changes the image from an array of images. This custom view works fine and is performant when I embed it within any other UIView. It changes images at the prescribed speed and doesn't have any issues. It works as I'd like.

This exact same view however, when placed inside a UITableVieCell that looks like the below, renders only the first image and does not change. I have confirmed that the timer is firing, and the the image view is changing its image under the hood by printing the reference to the UIImageView.image. That is to say, that when embedded within a UITableViewCell, my custom UIView is firing its timer and the UIImageView within this custom view, thinks it is changing its image. This leads me to believe that this is a display issue and not a data setting issue.

As a result, here is what have tried:

  1. I've tried wrapping it in DispatchQueue.main.async{}
  2. Adding a delegate so that my UIViewController can call tableView.reloadData() when the timer is fired
  3. Removing the setter of multiImageView.images on my prepareForReuse

Custom Cell Below


import UIKit
import SnapKit

final internal class MultiImageCell: UITableViewCell {
    // MARK: - UI Elements
    public var multiImageView: MultiPartImageView = MultiPartImageView()

    // MARK: - Lifecycle
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)

        //Setup Cell
        selectionStyle = .none

        //Setup Section Title View
        multiImageView.rotationSpeed = 0.3
        contentView.addSubview(multiImageView)

        //Setup Constraints
        setupConstraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        accessoryType = .none
        multiImageView.images = []
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        setupConstraints()
    }

    // MARK: - Functions
    private func setupConstraints() {
        //Setup Section View Constraints
        multiImageView.snp.remakeConstraints({ (make) in
            make.edges.equalToSuperview().inset(16)
            make.width.height.equalTo(240)
        })
    }
}

Setting Images on Cell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Check for Cell
        guard let row = viewModel.row(at: indexPath) else {
            return UITableViewCell()
        }

        // Setup Cell
        let cell = tableView.dequeueReusableCell(withIdentifier: row.cellReuseIdentifier,
                                                 for: indexPath)
        cell.textLabel?.text = viewModel.title(for: row, indexPath: indexPath)
        cell.detailTextLabel?.text = row.detailText
        cell.accessoryView = row.accessoryView
        cell.accessoryType = row.accessoryType ?? .none
        cell.imageView?.image = row.image

        switch RowStyle(rawValue: row.cellReuseIdentifier) {
        case .multiImageCell:
            guard let multiImageCell: MultiImageCell = cell as? MultiImageCell else {
                return cell
            }
            guard let multiImageRow: MultiImageRow = row as? MultiImageRow else {
                return cell
            }
            multiImageCell.multiImageView.images = multiImageRow.images
        default:
            break
        }

        return cell
    }

CodePudding user response:

It appears that this is a bug on the iOS 15 simulator on M1 processors. The code functions perfectly on a real device but not on a simulator. I intend to file a radar for this.

  • Related