UIStackView add arrange subviews inside tableView Cell didSet - swift

In my tableView Cell class, I use didSet method to setup my UI values and in there I have an array of string from api which I use to create an array of buttons and append it to a UIStackView
var pollDataInPollCell: PollModel? {
didSet{
if let pollOptions = pollDataInPollCell?.poll_detail {
//sample pollOptions = ["Button1","Button2","Button3","Button4"]
for index in pollOptions {
let bt = UIButton(type: .system)
bt.setTitleColor(.blue, for: .normal)
bt.setTitle(index, for: .normal)
optionBtnStackView.addArrangedSubview(bt)
}
}
}
}
my stackView outside of didSet
var optionBtnStackView: UIStackView = {
let sv = UIStackView()
sv.axis = .vertical
sv.distribution = .fillEqually
sv.spacing = 5
return sv
}()
everything works great at launch however, when I scroll down and up, my stackview is adding 4 more buttons. It has 4 at launch and then 8 and then 12 and keep increasing by 4 whenever I scroll up and down my app. I know the problem is from tableView dequeueReusableCell but I couldn't find a way to fix this issue. Any suggestions? Thank you.

Use below code in your didSet
var pollDataInPollCell: PollModel? {
didSet{
for view in optionBtnStackView.subviews {
optionBtnStackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
if let pollOptions = pollDataInPollCell?.poll_detail {
//sample pollOptions = ["Button1","Button2","Button3","Button4"]
for index in pollOptions {
let bt = UIButton(type: .system)
bt.setTitleColor(.blue, for: .normal)
bt.setTitle(index, for: .normal)
optionBtnStackView.addArrangedSubview(bt)
}
}
}
}

Related

How can I change ViewController with tapping an UIImageView?

I created a CollectionView. This view has a userProfileImageView and I want to change view controller to ProfilePage when clicked this user profile image. Try some code but neither present nor push view controller doesn't work to me. Can you help me ?
lazy var userProfileImageView: UIImageView = {
let useriv = UIImageView()
useriv.contentMode = .scaleAspectFill
useriv.clipsToBounds = true
useriv.layer.cornerRadius = 40 / 2
useriv.isUserInteractionEnabled = true
useriv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(userImageTap)))
return useriv
}()
#objc func userImageTap() {
print("useriv clicked")
let profilePage = ProfilePage(collectionViewLayout: UICollectionViewFlowLayout())
}

Swift Safe Area Layout not activating properly

I am learning some auto layout programatically and I want to add a button on the bottom part of the screen, just above the safe area.
I know the code is working, because I tested it in another project, I think it is a conflict because I get to this viewController from another one.
The code for my button
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
The code for setting up my constraints
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
Like I said this code is working in another project but I think it is in conflict because of how I called this viewController.
This is a code from the first view controller that make the new viewController(GettingStartedController) to be shown, here it will be the button mentioned above.
func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
view.addSubview(controller.view)
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}
I think the problem is here any ideas on how to change the call to the GettingStartedController so that will see the Safe Area the right way?
Check my code solution. I added your function to the button when it is tapped and it now presents the GettingStartedController():
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(switchVC), for: .touchUpInside)
return button
}()
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
#objc func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}

tableview custom cell button single selection/deselction

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.

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
}
}

Storing and Calling Index Path Swift

I have a collection view in swift in where it needs to store which button is pressed last. These buttons consist of A, B, C, D, this is like a quiz game.
Code:
extension QuestionCell {
func configure(with model: QuestionCellModel) {
factLabel.text = model.fact
questionLabel.text = model.question
let views = answersStack.arrangedSubviews
for view in views {
view.removeFromSuperview()
}
for (index, answer) in model.answers.enumerated() {
print(index)
let answerLabel = UILabel()
answerLabel.text = answer
answersStack.addArrangedSubview(answerLabel)
let answerButton = UIButton()
answerButton.tag = index
let imageNormal = UIImage(named: "circle_empty")
answerButton.setImage(imageNormal, for: .normal)
let imageSelected = UIImage(named: "circle_filled")
answerButton.setImage(imageSelected, for: .selected)
answerButton.setTitleColor(.black, for: .normal)
answerButton.addTarget(self, action: #selector(answerPressed(_:)), for: .touchUpInside)
answersStack.addArrangedSubview(answerButton)
}
}
So with this it is able to print the index, but it should know which was the last button they selected, when they were done (Buttons: A,B,C,D). Based on the index it should store assigned values of each collection view cell(There are 5 question in the quiz). I have not figured out a way on how to solve this question because I am not sure the computer will store multiple index's in the collection view(Like how would the computer know when you click the done with quiz button what was the last index it chose for each question?) I hope this makes sense!
Thanks