Xcode6 creating a new project with Swift to create code
Create a Viewcontroller inheritance Uitableviewcontroller
Involves the model, the controller
Model: Zlplace.swift
Class Zlplace:nsobject { var place = "" var visited = false}
Tableviewcontroller Controller
Import Uikitclass Viewcontroller:uitableviewcontroller {///static data array, storage model var arrs = [Zlplace] () override Func viewdidload () {super.viewdidload () Let Place2 = Zlplace () place2.place = "Zhang2" Arrs.append (PLACE2) Let Place3 = Zlplace () place3.place = "Zhang3" Arrs.append (PLACE3) Let Place4 = Zlplace () place4.place = "Zhang1" Arrs.append (Place4) Self.tableview. Reloaddata ()}//Data source method, returns how many groups override Func Numberofsectionsintableview (Tableview:uitableview), Int { return 1; }//Number of rows per group override func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int { return arrs.count; }//Line What content is displayed override Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)-> ; UITableViewCell {Let cell = Tableview.dequeuereusablecellwithidentifier ("Cell", ForindexpatH:indexpath) as UITableViewCell let place = arrs[indexpath.row] Cell.textLabel.text = place . place return cell; }//Click on each cell to trigger what event override Func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpa TH) {let-place = Arrs[indexpath.row] place.visited =!place.visited; Let cell = Tableview.cellforrowatindexpath (Indexpath) cell?. BackgroundColor = Uicolor.clearcolor () if (place.visited) {cell?. Accessorytype = Uitableviewcellaccessorytype.checkmark}else{cell?. Accessorytype = Uitableviewcellaccessorytype.none}}//Click the Edit button @IBAction func editing (sender:anyobje CT) {self.tableView.setEditing (True, Animated:true)}//Remove each cell override func TableView (TableView: UITableView, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath Indexpath:nsindexpath) { if Editingstyle = = uitableviewcelleditingstyle.delete{Arrs.removeatindex (indexpath.row) TableVie W.deleterowsatindexpaths ([Indexpath], WithRowAnimation:UITableViewRowAnimation.Top)}}
Basic use of TableView in Swift