美文网首页UITableView和UICollectionView
Swift 学习 第3天 - UITableViewCell

Swift 学习 第3天 - UITableViewCell

作者: 失忆的程序员 | 来源:发表于2022-08-11 10:05 被阅读0次
    UITableViewCell
    

    cell 实现

    //
    
    import UIKit
    import SnapKit
    
    class BaseTableViewCell: UITableViewCell
    {
        class func initWithTableViewCell(_ tableView: UITableView, _ indexPath: NSIndexPath, _ Identifier: String) -> BaseTableViewCell
        {
            var cell = BaseTableViewCell()
            cell = tableView.dequeueReusableCell(withIdentifier: Identifier, for: indexPath as IndexPath) as! BaseTableViewCell
            return cell
        }
        
        // 根据model 填充Cell
        func cellForModel(model: BaseModel?) {
            if let tempModel = model {
                nameLb.text = tempModel.name
            }
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            setupUI()
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        fileprivate lazy var nameLb : UILabel = {
            let nameLb = UILabel()
            nameLb.backgroundColor = UIColor.orange
            nameLb.text = "哈哈哈"
            nameLb.textColor = .white
            nameLb.font = .systemFont(ofSize: 14)
            return nameLb
        }()
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            // Configure the view for the selected state
        }
        
    }
    
    
    extension BaseTableViewCell
    {
        fileprivate func setupUI() {
            
            setupTitleLabels()
        }
        
        fileprivate func setupTitleLabels() {
            
            self.contentView.addSubview(nameLb)
            nameLb.snp.makeConstraints { make in
                make.left.equalTo(15)
                make.centerY.equalTo(self.contentView)
            }
            
        }
    }
    
    
    

    VC 实现

    //
    
    import UIKit
    import SnapKit
    
    private let NormalCellID = "BaseTableViewCell"
    
    class AVC: BaseVC {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = XColor.x_randomColor
            setupUI()
            AddNavUI()
        }
        
        // MARK: ----- lazy
        private lazy var tableView: UITableView = {
            let tableView = UITableView(frame: CGRect.zero, style: .plain)
            tableView.register(BaseTableViewCell.self, forCellReuseIdentifier: NormalCellID)
            tableView.separatorStyle = .none
            tableView.delegate = self
            tableView.dataSource = self
            //tableView.backgroundColor = XColor.x_randomColor
            return tableView
        }()
        
        private lazy var dataArys: NSMutableArray = {
            let dataArys = NSMutableArray()
            return dataArys
        }()
        
    }
    
    // MARK: ----- UI
    extension AVC
    {
        
        private func setupUI() {
            view.addSubview(tableView)
            tableView.snp.makeConstraints { (make) in
                make.top.equalTo(TopHeight)
                make.left.right.bottom.equalTo(0)
            }
            
            for i in 0 ..< 100
            {
                let model = BaseModel()
                model.name = "哈哈哈" + " \(i) "
                dataArys.add(model)
            }
            tableView.reloadData()
        }
        
        func AddNavUI() {
            
            let homeNav = XNavView()
            homeNav.titleName = "个人中心"
            homeNav.isBackBtnHid = false
            homeNav.onBackBlock = { isok in
                self.navigationController?.popViewController(animated: true)
            }
            homeNav.navLineColor = .red
            view.addSubview(homeNav)
            homeNav.snp.makeConstraints { make in
                make.left.top.right.equalTo(0)
                make.height.equalTo(TopHeight)
            }
            
        }
        
    }
    
    // MARK: ----- tableview 代理
    extension AVC: UITableViewDelegate,UITableViewDataSource
    {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            dataArys.count
        }
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            50
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell = BaseTableViewCell.initWithTableViewCell(tableView, indexPath as NSIndexPath, NormalCellID)
            cell.cellForModel(model: (dataArys[indexPath.row] as! BaseModel))
            return cell
            
    //        let cell = tableView.dequeueReusableCell(withIdentifier: "BaseTableViewCell", for: indexPath) as! BaseTableViewCell
    //        cell.textLabel?.text = "---------\(indexPath.row)"
    //        cell.backgroundColor = XColor.x_randomColor
    //        return cell;
        }
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
            let skinvc = ASkinVC()
            self.navigationController?.pushViewController(skinvc, animated: true)
        }
    }
    
    
    有写的不对的地方请开金口,👍🏻
    二哈吃得苦中苦方为狗上狗.gif

    相关文章

      网友评论

        本文标题:Swift 学习 第3天 - UITableViewCell

        本文链接:https://www.haomeiwen.com/subject/upvzwrtx.html