Swift-Implements TableView single-selection system style

Source: Internet
Author: User

Implement TableView single selection

Import UIKit

Class Viewcontroller:uiviewcontroller {

var tableview:uitableview!

Override Func Viewdidload () {

Super.viewdidload ()

TableView = UITableView (frame:cgrectmake (0, 0, Uiscreen.mainscreen (). Bounds.width, Uiscreen.mainscreen (). Bounds.height), style:. Plain)

Tableview.delegate = Self

Tableview.datasource = Self

Self.view.addSubview (TableView)

}

}

Extension Viewcontroller:uitableviewdatasource,uitableviewdelegate {

Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {

return 20

}

Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {

Let cell = UITableViewCell (style:. Default, Reuseidentifier: "Cell")

Cell.textlabel?. Text = "123"

return cell

}

Gets the path of the cell that will be selected

Func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) {

Uncheck the Style

Tableview.deselectrowatindexpath (Indexpath, Animated:true)

Gets the currently selected cell

Let cell:uitableviewcell! = Tableview.cellforrowatindexpath (Indexpath)

Return all cells

Traversal cancels all cell styles

var Arry = Tableview.visiblecells

For i in 0.. < Arry.count {

Let Cells:uitableviewcell = Arry[i]

Cells.accessorytype =. None

}

Set the selected cell style

Cell.accessorytype =. Checkmark

}

}

Swift-Implements TableView single-selection system style

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.