When you use TableView, the background color of the default selected cells is dimmed, as shown in the following figure:
1, using a custom background color
You need to customize the background view when UITableViewCell is selected and set its color
Import Uikit
Class Viewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {
var tableview:uitableview?
Override Func Loadview () {
Super.loadview ()
}
Override Func Viewdidload () {
Super.viewdidload ()
Create a table view
Self.tableview = UITableView ()
self.tableview!. frame = CGRectMake (0, 0, Self.view.frame.width,
Self.view.frame.height)
self.tableview!. delegate = Self
self.tableview!. DataSource = Self
To create a reused cell
self.tableview!. RegisterClass (Uitableviewcell.self,
Forcellreuseidentifier: "Swiftcell")
Self.view.addSubview (self.tableview!)
}
In this case, there is only one partition
Func Numberofsectionsintableview (Tableview:uitableview)-> Int {
return 1;
}
Returns the number of table rows (that is, returns the number of controls)
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
return 20
}
Create each cell display (create a parameter indexpath a specified cell)
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell
{
In order to provide tabular display performance, the completed cells need to be reused
Let identify:string = "Swiftcell"
Cells in the same form are reused and registered at the time of declaration
Let cell = Tableview.dequeuereusablecellwithidentifier (Identify,
Forindexpath:indexpath) as UITableViewCell
Cell.textlabel? Text = "entry \ (Indexpath.row)"
Select Background to change to green
Cell.selectedbackgroundview = UIView ()
Cell.selectedbackgroundview? BackgroundColor =
Uicolor (red:135/255, green:191/255, blue:49/255, alpha:1)
return cell
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
2, change the text color of selected cells
By using the TextColor and Highlightedtextcolor properties of the text labels within the cell, you can set the default and selected colors for the text.
The default text color is black, and the selected text is white
Cell.textlabel? TextColor = Uicolor.blackcolor ()
Cell.textlabel? Highlightedtextcolor = Uicolor.whitecolor ()