Wednesday, January 3, 2024
HomeiOS Developmentios - sendSubviewToBack shouldn't be working in UITableView

ios – sendSubviewToBack shouldn’t be working in UITableView


I need to modify tableView’s hierarchy. I used each sendSubviewToBack and bringSubviewToFront to verify RedCell is underneath BlueCell, additionally I create the RedCell first in order that BlueCell ought to past RedCell.(the overBgView in RedCell shouldn’t cowl BlueCell) Nonetheless, the end result reveals under, it is not working. So how to verify the RedCell is underneath BlueCell?:
enter image description here
My code is right here:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("<<< (indexPath)")
        
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RedCell.identifier, for: indexPath) as! RedCell
            // RedCell
            tableView.sendSubviewToBack(cell)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: BlueCell.identifier, for: indexPath) as! BlueCell
            // BlueCell
            tableView.bringSubviewToFront(cell)
            return cell
        }
    }
    
    non-public lazy var tableView: UITableView = {
        let desk = UITableView(body: .zero, model: .plain)
        desk.estimatedRowHeight = 80
        desk.showsVerticalScrollIndicator = false
        desk.separatorStyle = .none
        desk.delegate = self
        desk.dataSource = self
        desk.register(RedCell.self, forCellReuseIdentifier: RedCell.identifier)
        desk.register(BlueCell.self, forCellReuseIdentifier: BlueCell.identifier)
        return desk
    }()
    
    non-public lazy var label: UILabel = {
        let label = UILabel()
        label.textual content = "------split----the views under should not desk cell"
        label.textColor = .black
        return label
    }()
    
    non-public lazy var view1: UIViewOne = {
        let view = UIViewOne()
        return view
    }()
    
    non-public lazy var view2: UIView = {
        let view = UIView()
        view.backgroundColor = .blue
        return view
    }()
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        tableView.register(RedCell.self, forCellReuseIdentifier: "redCell")
        tableView.register(BlueCell.self, forCellReuseIdentifier: "blueCell")
        setupUI()
    }
    
    public init() {
        tremendous.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        tremendous.init(coder: coder)
    }
    
    non-public func setupUI() {
        view.addSubview(tableView)
        view.addSubview(label)
        view.addSubview(view1)
        view.addSubview(view2)
        
        tableView.snp.makeConstraints { make in
            make.prime.equalToSuperview().inset(200)
            make.left.proper.equalToSuperview()
            make.top.equalTo(120)
        }
        
        label.snp.makeConstraints { make in
            make.prime.equalTo(tableView.snp.backside).offset(30)
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
        }
        
        view1.snp.makeConstraints { make in
            make.prime.equalTo(label.snp.backside).offset(30)
            make.left.proper.equalToSuperview()
            make.top.equalTo(44)
        }
        view2.snp.makeConstraints { make in
            make.prime.equalTo(view1.snp.backside)
            make.left.proper.equalToSuperview()
            make.top.equalTo(44)
        }
    }
}

class RedCell: UITableViewCell {
    static let identifier = "RedCell"
    
    non-public lazy var overBgView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        return view
    }()
    
    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: model, reuseIdentifier: reuseIdentifier)
        backgroundColor = .pink
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
    
    non-public func setupUI() {
        contentView.addSubview(overBgView)
//        contentView.sendSubviewToBack(overBgView)
        self.superview?.sendSubviewToBack(overBgView)
        
        overBgView.snp.makeConstraints { make in
            make.prime.left.proper.equalToSuperview()
            make.top.equalTo(60)
        }
    }
}

class BlueCell: UITableViewCell {
    static let identifier = "BlueCell"
    
    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: model, reuseIdentifier: reuseIdentifier)
        backgroundColor = .blue
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
    
    non-public func setupUI() {
        
    }
}

class UIViewOne: UIView {
    non-public lazy var oversizeView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        return view
    }()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        backgroundColor = .pink
        addSubview(oversizeView)
        oversizeView.snp.makeConstraints { make in
            make.prime.left.proper.equalToSuperview()
            make.top.equalTo(60)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments