Yesterday we wrote about using the system cell how to create TableView, today we subdivide, is the different style of cell, how we write code. Create a cell yourself, inherit from UITableViewCell we'll see how the code in the cell is written.
Import Uikitclass Hometableviewcell:uitableviewcell {//We simply add a picture on this cell to let Oneimage:uiimageview = UIImage View ()//overriding initialization method override Init (Style:uitableviewcellstyle, reuseidentifier:string?) {Super.init (Style:style, Reuseidentifier:reuseidentifier)//Create UI Method Creatui () }//This method is also necessary to implement, and the overriding initialization method is implemented together. Required init? (Coder Adecoder:nscoder) {FatalError ("init (coder:) have not been implemented")}//method to create the UI, only a simple picture is written here. Func Creatui () {oneimage.frame=cgrectmake (0, 0, self.contentView.bounds.width, self.contentView.bounds.height) Self.contentview. Addsubview (Oneimage)} override func awakefromnib () {Super.awakefro Mnib ()//initialization Co} override Func setselected (Selected:bool, animated:bool) {Supe R.setselected (selected, animated:animated)//Configure The view for the selected state}}
Here's the code for the controller's face.
Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell { if ( Indexpath.section! = 1) { //System cell let Cell:uitableviewcell = Tableview.dequeuereusablecellwithidentifier ("Swiftcell", Forindexpath:indexpath) Cell.textlabel? Text= "You are really handsome" return cell } else { //Custom cell let Cellone:hometableviewcell = Hometableviewcell () cellone.oneImage.image = UIImage (named: "Screen shot") return CellOne } }
This is complete, and we'll take a closer look.
Swift 2.0 custom cell and different style of cell