swift 自定义cell -----------------------简单学习--------第一篇

来源:互联网 发布:李嘉诚撤资 知乎 编辑:程序博客网 时间:2024/06/05 05:03

1.自定义cell

////  FirstSwiftCell.swift//  SwiftDeKeYiCai////import UIKitclass FirstSwiftCell: UITableViewCell {        var titleLabel:UILabel?    var picImageVIew:UIImageView?        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {        super.init(style: style, reuseIdentifier: reuseIdentifier)        self.setUpUI()    }        required init?(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)    }        func setUpUI() {        self.titleLabel = UILabel.init()        self.titleLabel?.backgroundColor = UIColor.clear;        self.titleLabel?.frame = CGRect(x:65, y:0, width:100, height:50)        self.titleLabel?.textColor = UIColor.magenta        self.titleLabel?.text = "第一条显示的数据"        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)        self.titleLabel?.textAlignment = NSTextAlignment.left;        self.contentView.addSubview(self.titleLabel!)                self.picImageVIew = UIImageView()        self.picImageVIew?.frame = CGRect(x:15, y:5, width:40, height:40)        self.picImageVIew?.image = UIImage.init(named: "1.png")        self.contentView.addSubview(self.picImageVIew!)            }    }

2.cell添加tableview --创建tableVIew。viewController 代码

////  ViewController.swift//  SwiftDeKeYiCai////import Foundationimport UIKitclass ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {        var tableView:UITableView!    var array = ["111", "222", "333", "444"]    override func viewDidLoad() {        super.viewDidLoad()        let rect = self.view.frame        tableView = UITableView(frame:rect)        tableView.delegate = self        tableView.dataSource = self        let view = UIView()        tableView.tableFooterView = view        self.view.addSubview(tableView)                tableView.register(FirstSwiftCell.self, forCellReuseIdentifier: "cellid")            }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        func numberOfSections(in tableView: UITableView) -> Int {        return 1;    }        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return array.count;    }        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {        return 50;    }        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {                let cellid="cellid"        var cell:FirstSwiftCell = tableView.dequeueReusableCell(withIdentifier: cellid) as! FirstSwiftCell        if cell.isEqual(nil)        {            cell=FirstSwiftCell(style: UITableViewCellStyle.default, reuseIdentifier: cellid)        }        cell.selectionStyle = UITableViewCellSelectionStyle.none        cell.titleLabel?.text = array[indexPath.row]        return cell             }            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {            }}


4.简单的显示效果