Use Xib custom Tableviewcell look at
1. Custom Columns
Create a new Xib file Cartblcell, drag and drop a UITableViewCell, drag and drop a picture and a text box to Tableviewcell
And give our Xib a logo
In order to learn, my xib and backstage class are built separately. We'll build a cocoa touch class file name called Carcelltableviewcell inherits from UITableViewCell
And connect our xib with the new Carcelltableviewcell.
Create and xib the output of pictures and text boxes in Carcelltableviewcell
Import UIKitclassCarcelltableviewcell:uitableviewcell {@IBOutlet weak var cellimg:uiimageview!@IBOutlet weak var lbcell:uilabel!Overridefunc awakefromnib () {super.awakefromnib ()//Initialization CodeCellImg.layer.borderWidth =1CellImg.layer.masksToBounds=true //CellImg.layer.cornerRadius = } Overridefunc setselected (Selected:bool, Animated:bool) {super.setselected (selected, animated:animated) //Configure The View for the selected state }}
2. Associated Cell and TableView
1. Drag and drop a uitableview on the Main.storyboard and establish an output join in the background code
1. Registering xib in the Load event
2. In the Tableveiw method, get the current column, specifying the data source.
Import UIKitclassViewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {@IBOutlet var tableview:uitablevie W!var tabledata: [String]= ["BMW","Ferrari","Lambo"] Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib.Let cellnib = uinib (nibname:"Cartblcell", Bundle:nil) tableview.registernib (cellnib, Forcellreuseidentifier:"Cell") } Overridefunc didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.} func TableView (Tableview:uitableview, numberofrowsinsection section:int)-Int {returnTabledata.count} func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath) -UITableViewCell {Let Cell:carcelltableviewcell= Self.tableView.dequeueReusableCellWithIdentifier ("Cell") as!Carcelltableviewcell Cell.lbCell.text=Tabledata[indexpath.row] Cell.cellImg.image=UIImage (Named:tabledata[indexpath.row])returnCell} func tableView (Tableview:uitableview, Diddeselectrowatindexpath indexpath:nsindexpath) {PR Int ("\ (indexpath.row)")} func TableView (Tableview:uitableview, Heightforrowatindexpath Indexpath:nsindexpath)-CGFloat {return - }}
Source:customertablecelldemo.zip
"Swift Learning Notes" three. customizing UITableViewCell with Xib