"Swift Learning Notes" three. customizing UITableViewCell with Xib

Source: Internet
Author: User
Tags uikit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.