Swift changes the style of the table selected cell (background color, text color)

Source: Internet
Author: User

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 ()

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.