美文网首页
swift 3.0 新建UITableView自定义Cell

swift 3.0 新建UITableView自定义Cell

作者: 老KFirstK | 来源:发表于2016-11-30 16:25 被阅读0次

1没有注册

importUIKit

classViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{

varnameArr = ["嘿嘿嘿","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦"]

overridefuncviewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

let rectOne =CGRect(x:0, y:0, width:320, height:500)

lettableView =UITableView(frame: rectOne, style: .plain)

view.addSubview(tableView)

tableView.delegate=self

tableView.dataSource=self

}

functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

returnnameArr.count

}

functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{

varcell =

tableView.dequeueReusableCell(withIdentifier:"reuse")as?TableViewCellOne

ifcell ==nil{

cell =TableViewCellOne(style: .default, reuseIdentifier:"reuse")

}

cell?.appNameLabel?.text=nameArr[indexPath.row]

returncell!

}

functableView(_tableView:UITableView, heightForRowAt indexPath:IndexPath) ->CGFloat{

return200

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

2.有注册

importUIKit

classViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{

varnameArr = ["嘿嘿嘿","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦","哈哈哈","啦啦啦"]

overridefuncviewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

letrectOne =CGRect(x:0, y:0, width:320, height:500)

lettableView =UITableView(frame: rectOne, style: .plain)

view.addSubview(tableView)

tableView.delegate=self

tableView.dataSource=self

tableView.register(TableViewCellOne.self, forCellReuseIdentifier:"reuse")

}

functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

returnnameArr.count

}

functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{

letcell =

tableView.dequeueReusableCell(withIdentifier:"reuse")as!TableViewCellOne

cell.appNameLabel?.text=nameArr[indexPath.row]

returncell

}

functableView(_tableView:UITableView, heightForRowAt indexPath:IndexPath) ->CGFloat{

return200

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.}

}

总结:区别就是有注册的时候多这句代码tableView.register(TableViewCellOne.self, forCellReuseIdentifier:"reuse”),并且在cell方法里用letcell =

tableView.dequeueReusableCell(withIdentifier:"reuse")as!TableViewCellOne

cell.appNameLabel?.text=nameArr[indexPath.row]

returncell

as后是!号,第二句cell后不加?号,第三句cell后没有!

再看cell类里

importUIKit

classTableViewCellOne:UITableViewCell{

varpictureImageView:UIImageView?

varappNameLabel:UILabel?

requiredinit?(coder aDecoder:NSCoder) {

super.init(coder: aDecoder)!

}

overrideinit(style:UITableViewCellStyle, reuseIdentifier:String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

//overrideinit后没有代码提示,是手打,super.init后有代码提示

createVeiw()

//创建视图

}

funccreateVeiw() {

appNameLabel=UILabel.init()

appNameLabel?.backgroundColor=UIColor.cyan

appNameLabel?.frame=CGRect(x:20, y:0, width:self.frame.size.width-40, height:30)

self.addSubview(appNameLabel!)

pictureImageView=UIImageView()

pictureImageView?.frame=CGRect(x:10, y:40, width:self.frame.size.width-20, height:150)

pictureImageView?.image=UIImage.init(named:"33.jpg")

self.contentView.addSubview(pictureImageView!)

}

//    override func layoutSubviews() {

//        super.layoutSubviews()

//

//    }

overridefuncawakeFromNib() {

super.awakeFromNib()

// Initialization code

}

overridefuncsetSelected(_selected:Bool, animated:Bool) {

super.setSelected(selected, animated: animated)

// Configure the view for the selected state

}

}

相关文章

网友评论

      本文标题:swift 3.0 新建UITableView自定义Cell

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