swift纯代码自定义UITableViewCell —— Swift
swift纯代码自定义UITableViewCell —— Swift
今天学习一下用swift纯代码进行自定义UITableViewCell顺便自己做个笔记,其实用swift自定义tableViewCell的思想和Object-c是一样的,画重点那就直接上代码了:
自定义tableViewCell的部分:
import UIKit class FirstCustomTableCell: UITableViewCell { let imageView_W = 120.0//w:h = 4:3 let imageView_H = 90.0 let subView_interval:CGFloat = 10.0 var leftImageView : UIImageView? var nameLabel : UILabel? var subNameLabel : UILabel? var timeLabel : UILabel? var browseLabel : UILabel? required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.selectionStyle = UITableViewCellSelectionStyle.none self.createCellUI() } func createCellUI(){ leftImageView = UIImageView.init(frame : CGRect(x:15.0,y:5.0,width:imageView_W,height:imageView_H)) leftImageView!.backgroundColor = UIColor.lightGray self.contentView.addSubview(leftImageView!) leftImageView?.image = UIImage(named: "leftImg1.jpg") //name nameLabel = UILabel.init(frame: CGRect(x:Max_X(object:leftImageView!) + subView_interval,y:subView_interval,width:SCREEN_WIDTH - Max_X(object:leftImageView!) - 2 * subView_interval,height:25.0)) nameLabel?.textColor = UIColor.darkText nameLabel?.font = UIFont.systemFont(ofSize: 18) nameLabel?.text = "世界杯开幕"; self.contentView.addSubview(nameLabel!) subNameLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:Max_Y(object: nameLabel!) + 5,width:W(object: nameLabel!),height:25)) subNameLabel?.textColor = UIColor.darkGray subNameLabel?.font = UIFont.systemFont(ofSize: 15) subNameLabel?.text = "世界杯开幕"; self.contentView.addSubview(subNameLabel!) timeLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:100-25,width:W(object: subNameLabel!) * 0.6,height:20)) timeLabel?.textColor = UIColor.lightGray timeLabel?.font = UIFont.systemFont(ofSize: 13) timeLabel?.text = "2018-01-01 10:58" self.contentView.addSubview(timeLabel!) browseLabel = UILabel.init(frame: CGRect(x:Max_X(object: timeLabel!),y:Y(object: timeLabel!),width:W(object: subNameLabel!) * 0.4,height:20)) browseLabel?.textAlignment = NSTextAlignment.right browseLabel?.textColor = UIColor.lightGray browseLabel?.font = UIFont.systemFont(ofSize: 13) browseLabel?.text = "浏览:"+"50" self.contentView.addSubview(browseLabel!) } 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 } }
在VC中对cell进行注册:
import UIKit class FirstCustomTableCellVc: UIViewController,UITableViewDelegate,UITableViewDataSource { let cell_identifier:String = "FirstCustomTableCell" var myTableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.navigationItem.title = "自定义TableViewCell" self.creatViewUI() } func creatViewUI(){ self.myTableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.plain) self.myTableView.tableFooterView = UIView.init() self.myTableView.delegate = self self.myTableView.dataSource = self self.view .addSubview(self.myTableView) self.myTableView.register(FirstCustomTableCell.classForCoder(), forCellReuseIdentifier: cell_identifier) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let customCell = tableView.dequeueReusableCell(withIdentifier: cell_identifier, for: indexPath) return customCell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
最后实现的效果如下图:
代码中用到的几个自定义的获取view的frame的函数是我进行的一个简单的自定义可以参考下面的文件,后续会用swift专门写一个类目用于view的frame参数的获取和重置:
感谢您的访问!
若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/