Basic use of TableView in Swift

Source: Internet
Author: User

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)-&gt ; 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

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.