passing data from Custom UItableViewCell into ViewController - ios

Can this be done with the delegate method?
The reason I'm asking is because I have two buttons on the custom table cell. one deletes the cell and the other sends info from the table cell.m class into another view controller. How can I implement this properly?

Yes, you can setup a delegate relationship between a cell and the view controller, it follows the same structure and approach as a normal delegate relationship:
Create a protocol
Add a delegate property (weak)
Set the delegate when the cell is returned for display
nil the delegate when the cell is removed from display
Implement the delegate method(s) in the view controller
When you specify the delegate protocol methods, pass the pertinent information, something like:
- (void)tableCell:(UITableViewCell *)cell didTriggerButton:(UIButton *)sender;
with didTriggerButton set to an appropriate name for the purpose of the button. In this way the view controller can get the button if required and has access to the cell (so it can easily get the index path associated with it).

Related

"cellForRowAtIndexPath" for a tableview inside alertviewcontroller not called

I am adding a tableview inside an alertviewcontroller , I have given delegate and datasource to the table. All delegate and datasource methods except cellForRowAtIndexPath are called. I have return a static value for number of rows in section to prevent 0 rows. Still it is occurring.
You are not supposed to modify the view hierarchy of a UIAlertController. You’ll either need to create your own view controller that mimics an alert controller or find a framework that does that for you.
To quote the Apple docs:
The view hierarchy for this class is private and must not be modified.
I would suggest you to create a custom UIViewController, add required functionality and present it as a Form Sheet. Refer https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html

Forward call to delegate at runtime

I have several subclasses of UITableViewController which contain cells of type myCell (a subclass of UITableViewCell). These cells contain UITextFields and are the delegates for these textfields. However, in rare cases, I would like to catch some of the calls to the delegates in the tableViewController (in particular, textFieldShouldBeginEditing:).
Is there a reasonable way to achieve this without having to subclass my cell ?
Have your myCell class define its own protocol as well as a delegate property.
Then make the table view controller the delegate for each cell it creates. Then the table view controller can implement the cell's protocol methods.
Then the implementation of the cell class can call, when needed, the methods of its delegate.

WKInterfaceButton event handling in WKInterfaceTable

I have a WKInterfaceTable table view with a WKInterfaceButton.
How can I add a target action to the button from the table view. As there is no tag property I am not able to handle it.
If your WKInterfaceButton is contained within a row controller, here is one method to determine which row's button was tapped:
Add your WKInterfaceButton to the row controller and use interface builder to connect the button's action to your row controller class
Add a property to your row controller that allows you to reference your data (for example, a weak reference to your data or a tag)
Add a property to your row controller that allows you to set your interface controller as a delegate
Create a protocol for the delegate that allows you to pass the data reference
When initializing each row controller, be sure to set the data and delegate properties
When the button action is handled in the row controller, call the delegate method that you defined in your protocol. Something like:
- (void)rowController:(MyRowControllerClass *)rowController didSelectRowWithTag:(NSInteger)tag
Handle this delegate method in your interface controller to do whatever work is necessary.
I use this technique in my own Watch app, and it works very well.

Common pattern with segues and table views in iOS

I have the set up of a user clicking a UITableViewCell, this triggers a segue to a 'detail' view controller which is popped onto the stack.
First,
User Taps -> [myVC tableView didSelectRow...] - this is where I can work out which cell and therefore which model object my user wants to modify or access.
Second,
User Taps -> [myVC prepareForSegue...] - this is where I set up my detail view controller with the correct model object.
So, do I just store the selected model object in an instance variable between the two functions being called?
In prepareForSegue, the sender argument will be the cell, so you can work out which one was tapped at that point. You could also check the tableview's selectedIndexPath property.
You don't need to implement both functions.
I understand that you need to pass an instance of your model to your details view controller after you tap on a table view row. You have some ways to achieve that:
You can create a property in your view controller with a data type of your model. Let's name it for example firstObject. Then, inside didSelectRow you can set this property or update it according to your needs. Then, inside prepareForSegue you can pass this instance to your details view controller, by doing something like: detailsVC.detailsObject = firstObject.
Inside prepareForSegue, you can get the value of the current tapped table view row using indexPathForSelectedRow.row. Assuming that you have an array holding different objects of your model that you need them to be sent to your details view controller based on the tapped table view row index, you can do something like:
detailsVC.detailsObject = myObjectsArray[myTableView.indexPathForSelectedRow.row].
I hope that I was able to make it more clear for you.

Custom UITableViewCell Button selected - reload data

Alright let's say I have two ViewController, FirstViewController that contains a table and the CustomCell. I set up the action of the button in the CustomCell and it does what it is supposed to do but I want to call the [tableView reloadData] function in my FirstViewController.
What is the proper way of calling this function after the button is selected? Is there a way to set up something in FirstViewController that gets called when the Button in the other Class is selected?
You're best bet, if I understand your question correctly, is to use the Delegate Protocol in the second view, which returns a message to the first view controller.
See my answer here on how to setup a delegate:
How to declare events and delegates in Objective-C?

Resources