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