setting target for disclosure button on subclassed UITableViewCell - iphone

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.

Related

UITableViewCell segue created in Interface Builder will not fire

Currently, I have a basic UI set up in Interface Builder that features a UITableViewController, with a seque leading from the prototype cell to a detail view. My code dequeues the cell with the identifier I have set in Interface Builder, but when testing the app, a tap on the cell does nothing but turn it blue.
I want the segue to push the detail view on to my navigation controller's stack, but the segue simply won't happen. Why could this be?
Assuming you are working on storyboards (segues, etc.) there is a bug in the auto layout scheme of iOS. Hence the segue is not fired. However, you can simply give the segue and identifier in attributes inspector of the segue, and programmatically from your class you can use
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
in this method you can fire the segue in the following manner.
[self performSegueWithIdentifier:(NSString*) target:(id)];
Let me know if this helps you.
(Make sure your delegates and datasources are all set up correctly).

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

storyboard segue from a tablecell an its Detail Disclosure Button

I want to segue from a tablecell to two different viewcontrollers
if clicked in the cell or clicked on the cells Detail Disclosure Button.
If I ctrl-drag the second segue, the first is deleted.
What am I doing wrong?
Tnx
Mica
I don't think you can link a specific detail disclosure button w/ a segue. My storyboards are extremely programatic b/c I use alot of custom CGRect etc..
so I tend to use something like this:
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"ViewControllerIdentifer" sender:self];
}
for reference this is an extremely good example project on storyboards and tableviews. there's a link to the project at the bottom also.
http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2

Weird problem with iPhone UITableView cell not un-highlighting after clicking

I have an app I am working on, that has a table view with a few cells, and when you hit a cell, the nav controller pushes a new view. Pretty simple stuff. The problem is that when I hit on a row, the whole row highlights blue, and then remains blue all while the view is being presented by the controller, and then if I hit back to go back to the table view, the row is just still completely highlighted Even though I'm not pressing it or anything.
I have made a few apps with the table view before, and have never seen this behavior before, so I am at a loss as to what is going on. Is there some setting that I have set wrong somewhere?
Thanks
You need to deselect cell manually (probably in tableView:didSelectRowAtIndexPath: method):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Your code
...
}
If your view controller is a subclass of UITableViewController, it will automatically deselect the row after you hit the back button. If you don't subclass UITableViewController, you need to call deselectRowAtIndexPath:animated: when you want it deselected.

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

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.