Change default row background color on selection - ios

I have a UITableView that when a row is tapped a UIView pops up, everything is working fine but what I want is to be able to change the background color of the row when it's tapped to other color than the default gray. The standard behavior which is how I have it right now, behaves as follow, when I tap the row it changes the background color to gray and when I dismiss the popup UIView the background color changes back to the default white.
What I want is to be able to change the background color to blue when a row is tapped and changed it back to white when the UIView is dismissed.
I tried the following, which changes the color when the row is tapped but it doesn't change it back to white when the popup UIView is dismissed. In other words, it leaves the tapped rows with a blue background
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
/// Change color of the selected row
let selectedCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = UIColor.blue
}
How can I change the background of a cell when it's tapped and change it back to the default white when the popup UIView is dismissed?
FYI - I tried the didDeselectRowAt but it's never called.
Thanks

didDeselectRowAt is called when you tap another cell while the current is selected , you need to check viewDidAppear if the dismiss calles it or use a delegate otherwise and inside it add this code
tableView.visibleCells.forEach {
$0.backgroundColor = .white
}

try this you have to set selection style to none when you return cell cell.selectionStyle = .none
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Your identifier", for: indexPath)
cell.selectionStyle = .none
return cell
}

Related

Swift UITableView Cell changes image when scrolled

I have a image inside my tableview cells that turns black when the cell is selected as well as the default cell turning gray. Problem is when I scroll down the screen and scroll up again the images are turn back as though they are not selected being image "cellSelected". So, after scrolling selected cells should have the image "cellSelected" yet they have "cellNotSelected". How can I make sure my app remembers the selected cells images?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomerSelectsBusinessesCell
cell.selectedCell.image = UIImage(named: "cellSelected")
updateCount()
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomerSelectsBusinessesCell
cell.selectedCell.image = UIImage(named: "cellNotSelected")
updateCount()
}
You need to save the change of state in your data model somewhere, and then edit your data source methods (specifically cellForRowAtindexPath) to show the cell in its new selected state.
This is true of any change you make to a cell. If the cell scrolls off-screen, it gets recycled and any customizations you've made to it's views will not longer be associated with that indexPath any more. So when the user takes action on a cell, you always need to record the information in your data model.

Swift tableview cell radio button implementation

I need to place a radio button in tableview custom cell. whenever user clicks the tableview cell or button then radio button needs to work. I tried by using below code but didn't execute well.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TableViewCell
cell.country.text = self.animals[indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.none;
if selectedRows.contains(indexPath)
{
cell.radioButton.setImage(UIImage(named:"check.png"), for: .normal)
}
else
{
cell.radioButton.setImage(UIImage(named:"uncheck.png"), for: .normal)
}
return cell
}
Here's a great solution for creating radio buttons in a UITableView using a storyboard that requires zero code - and has 2 great Cool Tips!!
Make sure your table view is set to Single Selection, and to use Static cells.
Add a Basic cell, set the image to be your unchecked button image, and make sure the selection style is Default
Cool Tip # 1: Click on and select the cell's image view, and then set it's highlighted image to be your checked state. When the cell is highlighted or selected, the image view within will change to show its highlighted state.
Cool Tip # 2: Next, drag a UIView into the cell's content view, behind the text label. As you're using a basic cell, you won't be able to drop it directly into the cell, you'll need to drag it into onto the Document Outline on the left instead. Then hook this up to the cell's selected background view outlet. When a cell is selected (or highlighted), this view will be displayed in the background. In this case, we're going to use it to prevent the grey background appearing, so set its colour to Clear. Note that it doesn't matter what size the view is, and there's no need to set any constraints - it's automatically sized to match the cell at runtime.
Finally, duplicate this cell and change the text for each of your radio button options. Build and run, and you have code-free radio buttons!
In your TableViewCell class why don't you create a data source element and override the didSet for it. also in your data source for the UITableView I would recommend an array of something more than just a String.
I haven't compiled the below so this is just an idea.
import UIKit
class TableViewCell : UITableViewCell {
var data: Animal? {
didSet {
self.country.text = data.description
if (data.isSelected) {
self.radioButton.setImage(UIImage(named:"check.png"), for: .normal)
} else {
self.radioButton.setImage(UIImage(named:"uncheck.png"), for: .normal)
}
}
}
}
in your view controller you will of course have to set the isSelected property whenever a row is tapped.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var animal = self.animals[indexPath.row]
animal.isSelected = !animal.isSelected
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TableViewCell
cell.data = self.animals[indexPath.row]
}
and for your Animal maybe something like this:
struct Animal {
var description: String
var isSelected: Bool
}

Change custom cell content view color

I added a change theme button in my main view controller where I have a tiny table view. I can change every color except a table view cell's Content View background color, and I can't access it outside cellForRowAt.
What should I do? Is there anyway to trigger a function from my custom Cell to change the color?
You can add a property in your controller to determine the color of the cells. When you wanna change the color, you call tableView.reloadData(). This will make cellForRowAt be called on each visible cell, and you can change color in this delegate method.
Your viewController
YourViewController: UIViewController {
fileprivate var cellColor = UIColor.blue
// where you change color
func changeColor() {
cellColor = UIColor.red
// this will make the delegate method `cellForRowAt` be called on each visible row
tableView.reloadData()
}
}
cellForRowAt:
// delegate method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuse id", for: indexPath) as! YourCustomCell
cell.contentView.backgroundColor = cellColor
// anything else...
return cell
}
You can achieve this by implementing delegate, if you have custom cell class and access the delegate method to change content view color.

Static table view cells: Clear color not corresponding

I have the below controller with the cell's, content view's, and table view's backgroundColor set to .clear, however, there is still a white background which I can't figure out what it is corresponding to.
It is due to Your table view cell colour.
Select table view cell:
Set its background colour as clear.
I suggest you to debug it in your Xcode. To do this you can launch it on you device(or on simulator), click 'Debug View Hierarchy' button.
Then by Mouse Click + Dragging on the screen rectangle you can rotate all layers of your screen and see which view is causing that white background.
this white background is due to table view cell select table view cell and from navigator make its background clear. or you can do it in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCellClass
cell.backgroundColor = .clear
}
this will solve your problem...
:)

UITableViewCell styling resetting when scrolled off and back onto view

I currently have a TableView in my project, which is set up to turn a cell green when it is pressed, and back to clear if it is pressed a second time. However, if I scroll down to the bottom of the table view, and scroll back up, all my cells have been reset to their default clear colour.
I'm not sure how to go about fixing this issue, as anything I can find referring to it is in Objective-C rather than Swift. Any help and advice as to how to go about this would be great, thanks.
Everytime a UITableViewCell goes out of the screen, any function that you've written in the tableViewController/ViewController runs again.
for example in cellForRowAtIndexPath if you have a cell.setUpCell() or something similar, it will rerun and reset your values to the original values.
if you have a
var name = testName in your MainVC
and you update something in your cell, you should change the name in your mainVc too.
Every time you scroll or call tableView.reloadData() UITableView cells will reload. So, every time you select UITableViewCell, add selected index (indexPath.row) to an array(ex: selectedIndexArray) in your didSelectRowAt indexPath: delegate. If the cell you selected is already selected one, then remove the cell from selectedIndexArray.
And in your cellForRowAt indexPath: manage the cells using selectedIndexArray.
var selectedIndexArray:[Int] = [] //to save selected tableViewCells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let isSelected = false
for each in selectedIndexArray
{
if each == indexPath.row
{
isSelected = true
}
}
if isSelected == true
{
//set selected cell color
}
else
{
//set default cell color
}
}
You need to write the logic of adding and removing cell indexes in your didSelectRowAt indexPath:.

Resources