[SWIFT] Xcode12 tableView display error

After updating to Xcode12, tableView caused a display error in the simulator.

It seems that you can no longer addSubview directly to tableViewCell.

contentView.addSubview(...)

change to.

import UIKit

 class SearchCell: UITableViewCell {
     
    let collectionImageView: CustomImageView = {
        let iv = CustomImageView()
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = .boldSystemFont(ofSize: 18)
        label.textColor = .white
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
        
        contentView.addSubview(collectionImageView)
        collectionImageView.anchor(top: nil, left: leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 95, height: 95)
        collectionImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true        
        
        contentView.addSubview(titleLabel)
        titleLabel.anchor(top: topAnchor, left: collectionImageView.rightAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 8, paddingLeft: 8, paddingBottom: 8, paddingRight: 8, width: 0, height: 0)
        self.selectionStyle = .none
    }
        
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

reference

https://stackoverflow.com/questions/63947042/since-updating-to-xcode-12-i-am-not-able-to-place-any-uicontrol-inside-uitablevi

Recommended Posts

Xcode12 tableView display error
JSP error display from Servlet
[Rails] Display form error messages asynchronously
Workaround for Xcode 12 "Double-quoted include" error
Animated display of Swift error messages
[Rails] How to display error messages individually
How to display error messages in Japanese