美文网首页初见Swift 5.x 学习之旅
swift学习之旅--实现一个自定义cell的tableView

swift学习之旅--实现一个自定义cell的tableView

作者: Auditore | 来源:发表于2020-06-16 22:18 被阅读0次

    不说便宜话了,直接上代码

    image

    controller

    import UIKit
    let screen_width = UIScreen.main.bounds.size.width
    let screen_height = UIScreen.main.bounds.size.height
    let windowKey = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    func STATUSBAR_HEIGHT() -> CGFloat {return windowKey?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0}
    func RGB_COLOR(r:CGFloat, g:CGFloat, b:CGFloat) -> UIColor {return UIColor(red: r, green: g, blue: b, alpha: 1)}
    
    
    class TestViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
        let cellIdentifierOne = "TestOneCell.Type.self"
        lazy var tableView:UITableView = {
            var tableViewTemp:UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: screen_width, height: screen_height))
            tableViewTemp.delegate = self
            tableViewTemp.dataSource = self
            tableViewTemp .register(TestOneCell.classForCoder(), forCellReuseIdentifier: cellIdentifierOne)
            tableViewTemp.tableFooterView = UIView.init() //去掉尾部无数据的多余cell
            return tableViewTemp
        }()
        var array:[String] = ["纯代码自定义cell","nib自定义cell"]
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell:TestOneCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifierOne, for: indexPath) as? TestOneCell
            if cell == nil {
                cell = TestOneCell.init()
            }
            cell?.titleLabel.text = array[indexPath.row]
            cell?.selectionStyle = UITableViewCell.SelectionStyle.none
            return cell!
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 60
        }
        
        //cell 点击事件
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            switch indexPath.row {
            case 1:
                print("didSelectRow1")
                break
            default:
                print("default")
            }
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationItem.title = "UITableView by Jerold"
            self.view.backgroundColor = UIColor.white
            self.view.addSubview(tableView)
            
            
            
        }
        
    
    }
    

    cell

    import UIKit
    
    
    
    class TestOneCell: UITableViewCell {
        func commonInit() -> Void {
            self.contentView.addSubview(self.imageViewOne)
            self.contentView.addSubview(self.titleLabel)
        }
        
        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
        }
        
        lazy var imageViewOne:UIImageView = {
            var imageView:UIImageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            imageView.backgroundColor = UIColor.gray
            return imageView;
        }()
        
        lazy var titleLabel: UILabel = {
            var titleLab:UILabel = UILabel.init(frame: CGRect(x: 60, y: 0, width: screen_width - 60 - 32, height: self.contentView.frame.size.height))
            titleLab.textColor = UIColor.black
            titleLab.textAlignment = NSTextAlignment.left
            titleLab.numberOfLines = 1
            titleLab.font = UIFont.systemFont(ofSize: 16)
            return titleLab
        }()
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            commonInit()
        }
        
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            fatalError("init(coder:) has not been implemented")
        }
        
    }
    

    相关文章

      网友评论

        本文标题:swift学习之旅--实现一个自定义cell的tableView

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