iPhone SDK, UIButton on UITableView, delete row if button was pressed [duplicate] - iphone

This question already has answers here:
Detecting which UIButton was pressed in a UITableView
(26 answers)
Closed 2 years ago.
I am new in iPhone development and now I have simple question(may be not simple:). I have a UITableView, and in that UITableView I have for example 5 rows, every row has a UILabel and UIButton.
What I want to do is to delete one element from the array when my button is pressed. Button I create with tag, but that doesn't work. I do not want use didSelectRow; I want to delete an element from the array only if my button was pressed. (For example, if the third button was pressed I want to delete the third element of the array. Using the standard delete button does not work for me.

If you have a custom UIViewController to manage each cell (which I highly recommend), simply wire up the button to an IBAction and have that method remove the item from whatever data structure supplies the UITableView with rows, and then tell the UITableView about it.
Failing that, I suppose you could attach a tag to each button, indicating which row it belongs to, and wire them all up to a single IBAction in your table-view controller.

Related

Single tap to activate button in UITableView Swift Storyboard

I Have a UITableView with sections and rows, one cell for sections and another one for rows.
Each Section got one UIButton that is just a normal + "image". First time I open the app I need to click this button twice for it to react, after that it reacts on the first tap every single time.
Does anyone have a good solution to this or is it an Emulator bug?

Creating a dynamic add button swift

How would I be able to create a button that works similarly to the add phone button in the contacts app? As shown below, to replicate it, I have a button on the right side that the type is Add Contact (appears as a plus in a circle) and on the right is a label. I then put them together in a stack view.
How does the contacts app have one large (bar) button that holds the image and the text? When tapping on the button (in the contacts app) it highlights the whole bar. This makes me believe that might have done this using a UITableView.
What's the best way to create a similar (bar) button as they did? Would this be easier to replicate if I used a UITableView instead?
It's definitely a tableView.
They either constrained button's top to an empty tableView's bottom or they made this as an integral tableView and each time you didSelectRowAt it inserts a row at known position.
Suppose they either use a DataSource delegate method for rows insertion or a custom-made function wrapped in tableView.beginUpdates() and tableView.endUpdates() (reloads the table with animation)
you should not use stackview to wrap both button and right text.because you also need tap event.and stack view not giving tapping event.
I think you should place your button and label inside UIView and than Assing UIControl as Class in Inspecter(not UIView).than Controll drag from Your View to Class File and select TouchUPinside event.disabel user interaction of both button and Label otherwise event will not Fired.
and than Add above UIControll and other Component to either tableView or Vertical StackView.

How to insert button into Table view cell Swift

How do add button on each cell for table view cell so that it know which one is from.
Example:
Data have 2 products which is product A and product B, then I store them in array which is [product]. Inside the array, there have their detail which are name and price. So, I have successful making these into the table view.
The Question is how to insert button into the table view cell?
Example:
When button pressed, it know which row is pressed which is when press the 2nd row will be product B.
I'm newbie here. Need help, thanks.
You have two options:
Option one - easy, quick, bad:
create instance of the UIButton with the frame to position where you actually want the button to be placed. add tag property as well as the action. Action would be the same for the each button and you could use that tag to do the different actions.
Option two:
Create subclass of your UITableViewCell. Add all the element you want there. Create action for the button and you can set the target UITableViewCell or UITableViewController. If you choose UITableViewCell to be the target you will have to implement delegate, confront that delegate in the UITableViewController and implement that delegate function there. If you choose controller to be the target, you will have to send it to the cell. You will have to set tag as well.
I would use option two because it is scalable, easly maintained, and you have visual of the cell. Adding more components is easier as well.
I don't thing you need to put button or any other control in UITableViewCell to identify the selected cell.
All you need to do, is to implement following delegate method in to your class where you have implemented UITableView
tableView(_:willSelectRowAtIndexPath:)
tableView(_:didSelectRowAtIndexPath:)
and these methods will let you know selected UITableViewCell indexPath and you can do what you want to do on selection.
Note: do not forget to set your UIViewController as delegate to UITableView (almost like you did in case of DataSource).
Here's a Github repo you can refer, the code is not in good shape though.

I have the cell that a button I need to access is in...how do I access it? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access multiple buttons in access function?
I have two different buttons in the same cell in a TabelView (which has 10 rows each with 2 buttons) both set to 20% opacity. One called "button1" and the other "button2". When "button1" is clicked I run the action "button1clicked". In "button1clicked" I set the opacity of button1 to 50%. I need to set the opacity of button2 to 100%.
So I need to somehow be able to get a reference to button2 in the same cell as button1. I can get a reference to the cell via
UITableViewCell *clickedCell = (UITableViewCell *)[sender superview];
How can I use this or any other way so I can set the opacity of "button2" via something like
["somehow reference button2" setAlpha:.5];
Thank you!
Your problem is that you seem to be violating MVC by overcomplicating your code. In the UITableViewCell subclass, make those buttons properties (if they aren't already), and implement the actions that set their opacity within the very same Table Cell class, no need for anything else. If you need to interact with other objects, make them delegates of the cell.
I would rather create a subclass of UIView as the content view of table view cell. Then you can handle the opacity change inside the UIView.
I like the subclass solution, but the other traditional solution is to set the tag property for your two buttons you add to your cell and then you can retrieve later them via viewWithTag method.

How to delete a custom tableview cell? [duplicate]

This question already has an answer here:
Smooth reload of uitableview data when rows are deleted
(1 answer)
Closed 8 years ago.
i am using custom cell in uitableview cell.I am giving button on the right of the navigation bar .if user click on the button the delete cell option has to come like default cell delete option...can any one share code ...thanks in advance....
To delete a UITableViewCell, you should remove the data from the Array that populated the tableview. You can remove it using any logic, and the value of the row returned from the touch.
After that, a simple [tableView reloadData]; will do the trick.
Whenever you click the button corresponding the custom cell you should move the data of the next concurrent cells to their previous cell and then call [reloadData]
check this may help