How to add to an array when button is pressed? - swift

I've got custom buttons on a custom tableview cell, when button is pressed the image changes to checked/unchecked (see code below).
Issues is: How do I add the cell row data thats checked to an array that will be its own table (eg. a table with only checked data).
#IBAction func tickAction(sender: UIButton) {
println("SSSS")
if (sender.selected) {
sender.setImage(UIImage(named:"Unchecked.png"), forState: .Normal)
sender.selected = false
}
else {
sender.setImage(UIImage(named:"Checked.png"), forState: .Normal)
sender.selected = true
}
}

A solution would be to extend UITableViewCell into your own class that holds the other data or holds a reference to a model that holds the data.
Ideally, the view controller should hold the models and should get notified by a view that the action took place to act accordingly. The view controller should then add the data to the other array.

Related

Make a button clickable inside a viewCell, UIViewCollection

I have created a .xib view cell view and it contains a button, label, and some text inside, how do I make the button clickable and get the details of the view ie, the label, and text?
A way to do this is by adding a tag and target to the button.
cell.moreBtn.tag = indexPath.row
cell.moreBtn.addTarget(self, action: #selector(showClickedBtn(sender:)),
for: .touchUpInside)
After that, create a function that will receive the action.
#objc func showClickedBtn(sender: UIButton) {
print("Button \(sender.tag) Clicked")
}

set image for UIbutton selected state not working in swift 4

I have swift4 in my project and set an image for button state selected. But, when the button is in the selected state the image is not getting changed. I have changed the image in storyboard as well as code but nothing works.
ChkBoxBtn.setImage(UIImage(named: "unCheck"), for: [])
ChkBoxBtn.setImage(UIImage(named: "alertCheck"), for: .selected)
When a user taps a button the associated action will be called.
Within that action or method you have to change the .isSelected property of the concerned UIButton and set it to true
yourButton.isSelected = true
This will then reflect the image you have set in storyboard for the selected state of the UIButton
Create an action outlet to view controller, then add the below code. This helps you to switch between selected and deselected states easily.
#IBAction func ChkBoxBtnTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}

Swift - Button action not triggered in table view cell

I have a ViewController who contains a TableView. I want in each cell, a red button who did something (let's say 'hello' for test), or when I touch the cell anywhere but not on the button I perform a segue.
But even when I touch the button, it's the segue who perform. I tried some search on SF but none of the questions help me...
I don't know if that's usual but when I touch the cell, all the row white background become gray, even the background of the red button.
In storyboard I have this cell hierarchy :
The user interaction is enabled on both cell and button.
In the questions I found they said, on the cell set 'accessory' on 'disclosure indicator', I did it, but if it's possible I would like to keep it at 'None'.
Also I set a custom class for the cell, here is the code :
class MyTableViewCell: UITableViewCell {
#IBOutlet weak var rentButton: UIButton? // I use this class for 2 tableview almost similar, but in the other I don't have the redButton, that's why I put an '?'
}
I also set the delegate and datasource of the tableView to my ViewController, here is the code of my view controller :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell
cell.rentButton!.tag = indexPath.row
cell.rentButton!.addTarget(self, action: #selector(MyViewController.rent(_:)), forControlEvents: .TouchUpInside)
return cell
}
// Rent() is never called:
func rent(sender: UIButton) {
print("here \(sender.tag)")
}
EDIT : The solution was to put out the button from the conainer view !
You can stop the segue from actioning when the button is pressed by performing it programatically. You can do the following:
Remove your segue that goes from the cell to another view controller and change it go from the TableViewController to the other view controller
Create a variable in your TableViewController class such as: var buttonPressed = false
In your button action set buttonPressed to true
In didSelectRow, check that buttonPressed is not true then performSegue and change buttonPressed to false, else do nothing
As discussed, move the button from the container to the content view

setEditing method delete button doesn't work

In my UIViewController, I have an UITableView which contains different cells with saved contents by using the NSUserDefaults. I also have a custom UIBarButtonItem which causes the UITableView to enter and leave editing mode.
This all works, but the problem is when I tap the round red button and on the right side of the cell appears the delete button, the delete button doesn't work... I can tap on it as much as I want but the cell does not get deleted.
#IBAction func EditListButton(sender: UIBarButtonItem) {
if ShoppingListTable.editing {
ShoppingListTable.setEditing(false, animated: true)
self.EditListButton.title = "Edit"
} else {
ShoppingListTable.setEditing(true, animated: true)
self.EditListButton.title = "Done"
}
}
Use UITableViewDataSource's tableView:commitEditingStyle:forRowAtIndexPath: to detect a tap on the delete button.
Then in the implementation delete the row from your data source (NSUserDefaults in your case).
So in your tableView's dataSource (ie. in the class you implemented numberOfSectionsInTableView: for example), add your custom implementation of tableView:commitEditingStyle:forRowAtIndexPath: where you delete the underlying data.

Swift - segue from custom cell and cell data passing to another controller

I'm stuck now for hours with a problem when trying to pass data from a tableview custom cell to another viewcontroller.
To be more precise:
TableViewController1 has CustomCell1 as a Prototype Cell class
CustomCell1 has Button1, Button2, Label1 and Label2
ViewController2 has Label3
The TableViewController1 is populated fine and has 10-20 rows of data and everything is displayed fine on screen.
When the user taps on Button1 or Button2 (from what I've seen this triggers the IBAction of the button and not selecting the row) I trigger a segue programmatically from the IBAction of the button I've added to TableViewController1.
What I can't figure out is how to pass the Label1 text when user taps Button1 and Label2 text when user taps Button2 to ViewController2 and display the appropriat text in Label3.
As there is no row selected in the tableview how can this be done if possible?
Thank you!
Kostas
Make sure you have prepared your segue properly. That was my hangup when I first tried.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showTea"){
let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
let genView:genViewController = segue.destinationViewController as! genViewController
genView.row = selectedIndexPath.row
}
else if (segue.identifier == "addTea"){
}
}
Then in my genViewController, I added the var row to hold the value for the row so when I pulled the data in genViewController it would pull the correct data.
var row: Int = 0
See my question on this subject and hope this helps.
core data swift sugue not pushing data to second view controller