Dynamically add a new row as Add in UITableView in edit mode? - iphone

I have a table view with 0 or n rows of data from datastore. I added a customized Edit button on the right of the view's navigation bar. By default, when the edit button is clicked, in the action event, I set the view as edit mode:
[self.tableView setEditing:YES animated:YES];
I would like to add a row at the end of the table view with Add button as an accessory on the left when the table view is in edit mode. And the "Add" row will not be displayed when the view is not in edit mode. This is very similar to the case of iPhon'e Contacts application when a contact is in edit mode.
I am not sure if I need to add a row dynamically, and how if so? An alternative way I guess is to add more row when tableView:numberOfRowsInSection: is called? If later is the case, I have to make it hidden when the view is not in edit mode and visible when the view is in edit.
By the way, my table view is loaded from xib file. Not sure if there are any settings there to specify the table view's style as UITableViewCellEditingStyleDelete and UITableViewCellEditingStyInsert to enable the add feature?

You need to implement a bunch of delegate methods.
On the UITableViewDelegate side:
- (UITableViewCellEditingStyle) tableView: (UITableView *) tableView
editingStyleForRowAtIndexPath: (NSIndexPath *) indexPath
On the UITableViewDataSource side:
- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath: (NSIndexPath *) indexPath
You can just add new rows to your data source, and then call [tableView reloadData] or [tableView insertRowsAtIndexPaths:...].
HTH.

Related

How do I make a table view cell work as a button?

I am trying to make an app that the user will press a cell and the cell will function as a button, now how do I make the cell function as a button and do an action or how do I know which cell is being pressed?
Please read the document but basically when you set the delegate of the tableviewcontroller it calls this method when clicked:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
The indexpath holds the section and the row. If you are using only one section then indexPath.row will return the index of the cell that was clicked.
You implement the UITableViewDelegate method tableView:didSelectRowAtIndexPath:.
That method will be called whenever the user taps any of the cells. If the passed indexPath is the index path of your button cell, then you do whatever should happen.
Read Apple's documentation on UITableViewDelegate
And you will find tableView:didDeselectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
this method gets called when the user taps a cell. indexPath.section and indexPath.row are your cell section and row indexs.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
If I understood your question correctly, you simply want to run some code when a user touches a cell.
The UITableViewDelegate has a method called tableView:didSelectRowAtIndexPath: (UITableViewDelegate tableView:didSelectRowAtIndexPath:)
Just implement that and you can do what you want in there.

TableView delegate method called only after a second cell is selected

I'm dealing with a problem that is really making me crazy. I've got a table view added to a view using IB. This tableView has a delegate and a datasource. The delegate is the view controller, the data source is another class that packs information for displaying them. The data source works and fill the table with correct data.
I'm not able to say the same for the delegate. The delegate implements the classic - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath and I have an NSLog inside that method that logs the cell selected.
The problem is that if I selected a cell, the cell changes its color in blue(selected state), but it doesn't trigger the delegate methods. If I press another cell now the new selection on a new cell pushes the detailviewcontroller with the right informations.
If I pop the cell is deseleted- since in viewWillAppear I put a method for deselect selected cell-, if I press a cell again nothing happens just blue selection,if I press another one it pushes the detail view.
I tried to make few changes to make the case easy as possible:
.used default styled cells
.added the tableview programmatically
.check the delegate of the tableview in different part of the code
Everything seems right I really can't understand.
Use
(void)tableView:(UITableView *)tableView selectRowAtIndexPath:(NSIndexPath *)indexPath
instead of
(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

setting target for disclosure button on subclassed UITableViewCell

I've created a subclass of UITableViewCell to create some custom appearance and UI functionality ('swipe to reveal delete button').
Some of the cells are disclosure/detail disclosure type, and have a UIButtonTypeDetailDisclosure etc added manually. I cannot use the table view controllers' UITableViewCellAccessory for row at index path method.
The question is, how do I set the target of these manually added buttons so that they correctly send the didSelectRowAtIndexPath to their table view controller?
There is no direct way of setting an action the accessory disclosure button. Though you can do this with delegate method
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
See if that works.

Iphone -- push a view controller onto the navigation stack when a button is clicked in a UITableViewCell

When a button is pushed in one of my app's table view cells, I need to push a certain view controller onto the navigation stack.
This could be done by using an instance of NSNotification to inform the table view's controller of the button press. But that would be awfully heavyweight, especially since selections in a tab bar in the app could cause the table view to appear or disappear, creating additional overhead as the various table views register and unregister themselves whenever they are tabbed onto or off of the screen.
Can anyone think of a better solution?
Why not put
[[self navigationController] pushViewController:targetViewController animated:YES];
in the method called by the button?
Make your UITableViewController use the UITableViewDelegate Protocol and implement this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
From the indexPath you can get which row has been pressed and then you know which cell is being selected. The purpose of the UITableViewController is to know about the cells and the cell itself does not need a button to trigger an event to push a new view.
What I did was set the table view's delegate to be the same as its controller. Then:
UITableView *myTableView = (UITableView *)self.superview;
NSIndexPath *indexPath = [myTableView indexPathForCell: self];
MyTableViewController *myTableViewController = (MyTableViewController *)(myTableView.delegate);
[myTableViewController buttonWasPressedOnCellWithIndexPath: indexPath];

How can I call the parent TableView to reload

In this iphone app I'm trying to make it so that when you select a table cell it saves that action, pops the current view controller and calls the previous view controller (which is and always will be a TableView) to reload it's cells with new data.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//update data code here
[self.navigationController popViewControllerAnimated:TRUE];
//Here is where I want the code to reload the view [parentTableView reload]; or something }
That's what I have at the moment when you select a cell in the current Table View
You need need to send -reloadData to the UITableView:
[tableView reloadData];
Edit : So just to clarify, you have a parent UITableViewController which pushes a child UITableViewController onto the navigationController? The code in your sample is implemented in the didSelectRowAtIndexPath in the child view controller?
If this is the case, you need to to add a property to your child UITableViewController which stores a pointer to its parent. Make sure this isn't a reference counted property because that will result in a circular reference. So before you pop your child UITableViewController it can send -reloadData to the tableView property of its parent.