Although the Tableviewcell features in the SDK are already powerful, many times, we still need a custom cell to meet our own needs. Recently studied how to use Swift to implement custom Tableviewcell, record it.
1.
Click the plus sign in the lower left corner to add a new class
XCode6.3 made some minor changes, integrated it, clicked file, and then went on to the next step:
2.
Here you can give your own Tableviewcell to change the name, remember to "Also create XIB file" Before the check box selected
3.
Design the Xib style you want. AutoLayout is very powerful, more use of a use of slowly skilled, can save a lot of code volume.
Stationtableviewcell.swift files do not need to be changed substantially
Here are the key steps to add in TableView to the Tableviewcell that we just customized
4.
In the storyboard we set up the Viewcontroller to carry TableView (projectdetail ... Controller.swift) For each property
Note that the TableView style is set to grouped, so there's a gap at the top, but don't worry, we'll fix this in the next code.
5.
viewdidload:
Override Func Viewdidload () { super.viewdidload () //Do any additional setup after loading the view. Let appdelegate = Uiapplication.sharedapplication (). Delegate as! Appdelegate appdelegate.projectdetail = self self.tableView.delegate = self self.tableView.dataSource = Self //Remove the blank of the header of the table view, mind the height must is more than 0 self.tableView.table Headerview = UIView (frame:cgrectmake (0, 0, self.tableView.frame.size.width, 0.01)) //Register the custom TableView Cell var nib = uinib (nibname: "Stationtableviewcell", Bundle:nil) Self.tableView.registerNib (NIB, Forcellreuseidentifier: "Cell") }
In order to click on the button in the cell to achieve the page jump, I have just initialized the interface, in the appdelegate inside the instance of a current Viewcontroller instance. If this process is not possible, please see my next blog, I will detail how to implement.
Next, set the TableView delegate and DataSource agents
And then just mentioned, remove the top of the blank code:
Self.tableView.tableHeaderView = UIView (frame:cgrectmake (0, 0, self.tableView.frame.size.width, 0.01))
Note that the height of the frame can not be
0, if it is 0 is no effect, must be more than 0, but we set this value is particularly small, with the naked eye can not be seen, so in disguise to achieve the removal of the top of the blank role. So we set it up as
0.01 .
The next most critical step is to initialize the custom cell
var nib = uinib (nibname: "Stationtableviewcell", Bundle:nil) Self.tableView.registerNib (NIB, Forcellreuseidentifier: "Cell")
6.
Implementing two Proxy methods
#MARK: TableView Delegate func tableview (Tableview:uitableview, Heightforrowatindexpath Indexpath:nsindexpath )-cgfloat { return] func tableView (Tableview:uitableview, Didselectrowatindexpath Indexpath: Nsindexpath) { //To do } //#MARK: TableView DataSource func Numberofsectionsintableview (tableview: UITableView), Int { return 1 } func TableView (Tableview:uitableview, numberofrowsinsection section : int), int { return 5 } func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath: Nsindexpath)-UITableViewCell { //all the custom cells into the TableView var cell = Tableview.dequeuereusa Blecellwithidentifier ("cell", Forindexpath:indexpath) as! Stationtableviewcell return Cell }
These agent methods are not much to say, very commonly used.
All the steps are done here, run the program, and see what your custom looks like.
Swift implements a custom Tableviewcell