tableview custom cell button single selection/deselction - swift

i have been coding for a while i am stuck with tableview single selection/deselection.
I have a tableview with custom cell which has a button. if i click on one button another button in bottom gets selected as well.For example when i click on button with index 2 another button gets click in the button.
it should be like when i click on one button other buttons should be deselected.
Thanks!
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected == true {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.isSelected = false
}
else {
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
cell.EventReviewQuickeReviewBtn.isSelected = true
}
}

Cells get reused. You need to ensure the exact same set of properties are set/reset in both conditions.
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.white // or whatever
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.red, for: .normal) // or whatever
} else {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.black // or whatever
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.green // or whatever
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 3 // or whatever
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
}
cell.EventReviewQuickeReviewBtn.isSelected = !cell.EventReviewQuickeReviewBtn.isSelected
}

when you select any button then you should have mark isSelected property to false
assuming you have a list of models for your table view like
class Example{
...
var isSelected: Bool
...
}
(in ViewController)
let list: [Example] = [example1, example2, example3]
on tap of any button deselect all then selected desire button like
list.forEach { (example) in
example.isSelected = false
}
list[selectedButtonIndex].isSelected = true
and then reload your tableView.

Related

How can I change my custom cell background when pressing a button?

I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.

Swift UIButton titleColor change when highlighted?

I would like to titleColor of my UIBUtton to change when the user taps on the button, with the below code currently the color of the UIBUtton title changes if the user press and hold on the button, however, not when tapped quickly, could you please let me know what do I need to change in the below code to make the titleColor change upon a tap detection only:
var resetFiltersButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Clear Filters", for: .normal)
button.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Bold", size: 20)
button.setTitleColor(.blue, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.titleLabel?.numberOfLines = 1
button.tag = 1
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc func buttonPressed(_ sender: UIButton) {
// MARK: - Clear Filters button pressed:
if sender.tag == 1 {
}
}
Why don't you try implementing that inside the buttonPressed function?
#objc func buttonPressed(_ sender: UIButton) {
sender.setTitleColor(color: UIColorUI, for: UIControl.State)
}
This issue is that you are using setTitleColor for .normal and .highlighted states.
.normal is when the button is not clicked.
.highlighted is while the button is being clicked on.
Instead you could:
Just updated the .normal color when pressed (in buttonPressed); or
setTitleColor for .selected for after it's clicked and handle the selection state within buttonPressed (add sender.isSelected = !sender.IsSelected)

How to highlight one part of a tableview cell in swift?

I have two clicks of gesture recognizer in my tableview cell to access different storyboards.
My second click is in the extreme right of my cell and I want when I click on it to just highlight that button and not the whole cell which includes the other button
So my question is : how to separate highlights of different clicks in a tableview cell ?
First you need to set the
tableView.allowsSelection = false
You can add two views in the cell and add tap
func setAccessoryViewTapGestures() {
var tapGestureforLeftAccesoryView = UITapGestureRecognizer()
tapGestureforLeftAccesoryView = UITapGestureRecognizer(target: self, action: #selector(leftAccessoryViewTapped(_:)))
tapGestureforLeftAccesoryView.numberOfTapsRequired = 1
tapGestureforLeftAccesoryView.numberOfTouchesRequired = 1
leftAccessoryView.addGestureRecognizer(tapGestureforLeftAccesoryView)
leftAccessoryView.isUserInteractionEnabled = true
var tapGestureforRightAccesoryView = UITapGestureRecognizer()
tapGestureforRightAccesoryView = UITapGestureRecognizer(target: self, action: #selector(rightAccessoryViewTapped(_:)))
tapGestureforRightAccesoryView.numberOfTapsRequired = 1
tapGestureforRightAccesoryView.numberOfTouchesRequired = 1
rightAccessoryView.addGestureRecognizer(tapGestureforRightAccesoryView)
rightAccessoryView.isUserInteractionEnabled = true
}
Here rightAccessoryView and leftAccessoryView are the view in the cell
#objc private func leftAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
#objc private func rightAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
I think this helps

How to programmatically add tags to a UIButton in Swift

I know how to add tags in main.storyboard files, but now I am working on creating everything programmatically. However, I couldn't find anything online that tells me how to add tags to a button. I tried to read the apple documentation but don't know how to implement it.
Here is for example how I created my button, how can I add a tag to it, so that when I make multiple buttons and want to link them to a single action I can know what button has been pressed?
let buttonOne() : UIButton {
let button = UIButton(type: .system)
button.backgroundColor = .red
return button
}()
Try this:
let buttonOne() : UIButton {
let button = UIButton(type: .system)
button.backgroundColor = .red
button.tag = 2
return button
}()
Declare The Button
let menuChooseButton = UIButton()
menuChooseButton.frame.origin.y = 0
menuChooseButton.frame.origin.x = xPosition
menuChooseButton.frame.size.width = subViewWidth
menuChooseButton.frame.size.height = subViewHeight
menuChooseButton.backgroundColor = .clear
menuChooseButton.setTitle("", for: .normal)
menuChooseButton.tag = i
menuChooseButton.addTarget(self, action: #selector(menuSelectAction), for: .touchUpInside)
menuScrollView.addSubview(menuChooseButton)
Define Button Action
#objc func menuSelectAction(sender: UIButton!){
var tagNo: Int = sender.tag
if tagNo == 0{
// Whatever you want
}
}

Changing colour on buttons in tableview cell edit actions

Hello!
Having a go at swipe-to-delete functions and the likes in tableView cells today...
When toggling a button I would like it to change colours from green to red and back again. - Any suggestions on how to achieve this?
For the toggle button I have declared a boolean which is set
to false at start.
and this is what I've got inside editActionsForRowAtIndexPath:
...
let toggleAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Toggle", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let button: UIButton = UIButton()
button.backgroundColor = UIColor.greenColor()
if(self.toggleBool == false) {
self.toggleBool = true
button.backgroundColor = UIColor.redColor()
} else {
self.toggleBool = false
toggleButton.backgroundColor = UIColor.greenColor()
}
self.view.addSubview(button)
})
return [deleteAction, toggleAction]
}
The code snippet above does not make the button change colour when pressed!
Any help in the matter would be much appreciated!