apple/UIKit & ReactiveX

UITableView isSelected, isHighlighted 정리

lgvv 2024. 8. 16. 01:53

UITableView isSelected, isHighlighted 정리

 

UITableView에서도 UICollectionView와 동일하게 isSelected가 존재

  • 하지만 UICollectionView와 다르게 초기에 셀이 그려질 때 처음에 세팅되고 이후에는 호출되지 않음.

UITableView사용성

 

영상을 보면 보이듯이 처음에 isSelected가 호출된 후 그 이후로는 선택해도 해당 변수의 get-set 부분이 호출되지 않음.

 

예제코드

import UIKit
import SnapKit

final class SmapleTableViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let items = Array(1...100)
        tableView.configure(items)
    }
    
    private let tableView = SampleTableView()
    
    private func configureUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
    }
}

final class SampleTableView: UITableView,
                             UITableViewDelegate,
                             UITableViewDataSource {
    
    var array: [Int] = []
    
    func configure(_ items: [Int]) {
        self.array = items
        
//        self.reloadData()
    }
    
    // MARK: - Initialize
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        
        self.register(SmapleTableViewCell.self, forCellReuseIdentifier: SmapleTableViewCell.id)
        delegate = self
        dataSource = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(
            withIdentifier: SmapleTableViewCell.id,
            for: indexPath
        ) as? SmapleTableViewCell else { return .init() }
        
        let number = array[indexPath.row]
        cell.configure(number)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt| \(indexPath)")
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print("didDeselectRowAt| \(indexPath)")
    }
    
}

final class SmapleTableViewCell: UITableViewCell {
    static let id = String(describing: SmapleTableViewCell.self)
    
    override var isSelected: Bool {
        willSet {
            print("isSelected 호출")
            self.backgroundColor = newValue
            ? .red
            : .gray
        }
    }
    
    override var isHighlighted: Bool {
        willSet {
            print("isHighlighted 호출")
            self.backgroundColor = newValue
            ? .red
            : .gray
        }
    }
    
    func configure(_ number: Int) {
        numberLabel.text = "\(number)"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configureUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UIComponents
    
    private lazy var numberLabel: UILabel = {
        $0.font = .systemFont(ofSize: 30, weight: .bold)
        $0.textAlignment = .center
        return $0
    }(UILabel())
    
    private func configureUI() {
        contentView.addSubview(numberLabel)
        numberLabel.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
    }
}

#Preview {
    SmapleTableViewController()
}