Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of customizing UITableViewCell background Color by Swift

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "Swift custom UITableViewCell background color method", the content is detailed, the steps are clear, the details are handled properly, I hope this "Swift custom UITableViewCell background color method" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Effect.

Realize

You can implement the following methods in the subclass file (CustomTableViewCell.swift) of UITableViewCell

Override func setSelected (selected: Bool, animated: Bool) {super.setSelected (selected, animated: animated) / / Configure the view for the selected state if selected {self.backgroundColor = UIColor.orangeColor ()} else {self.backgroundColor = UIColor.whiteColor ()}

Run the project and you may find that when you click on the cell, the selected style is still the system style, as shown below:

What is the cause of this? When we open the view level, we will find that we have successfully set it, but it is covered, as shown in the following figure:

How should it be solved? In fact, it is very simple, you only need to modify the selectionStyle property of cell, as follows:

Cell.selectionStyle = UITableViewCellSelectionStyle.None

Now, we have finished customizing the cell selection style, isn't it very simple?

Extend

Sometimes there may be such a requirement, that is, I do not need to select the background color, but I want to flash when I click on a cell, that is, OK with a sudden change in the background color.

First of all, we need to implement the following method: when the cell is selected or unselected, it is set to white.

Override func setSelected (selected: Bool, animated: Bool) {super.setSelected (selected, animated: animated) / / Configure the view for the selected state if selected {self.backgroundColor = UIColor.whiteColor ()} else {self.backgroundColor = UIColor.whiteColor ()}

Second, in the proxy method, do the following:

Func tableView (tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {let cell = tableView.cellForRowAtIndexPath (indexPath) / / change the cell background color cell?.backgroundColor = UIColor.redColor ()}

In addition to operating in the proxy method, it can also be implemented in custom cells with the same effect, but it does not need to be implemented through the proxy method, as shown below:

Override func setHighlighted (highlighted: Bool, animated: Bool) {if highlighted {self.backgroundColor = UIColor.redColor ()} else {self.backgroundColor = UIColor.whiteColor ()} read here, this article "the method of Swift customizing UITableViewCell background color" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, Welcome to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report