Raise selection event (didSelectRowAtIndexPath) when subview of UITableViewCell is tapped - iphone

I've created a custom UITableViewCell that includes a number of subviews. In most cases I want the controller for the UITableViewCell to handle the events. In one case I want the subview to simply pass on the event to the parent UITableViewCell which will cause it to invoke didSelectRowAtIndexPath on the assigned delegate.
To put it another way: I want the subview to become "event transparent" -- I want events to pass over / through it and register with the underlying cell.
Any suggestions? In other languages I would raise an event. New to Objective-C and Cocoa and haven't been able to google up an answer yet.
Thanks in advance.

Try setting your view's userInteractionEnabled property to NO.
This will make it ignore all touch events, and then the views under it will be able to catch these events.

A couple of ways come to my mind:
I believe the translation for "raise an event" would be to send a notification, in which case you could look into NSNotification and NSNotificationCenter. Pretty simple.
Optionally, you could add a reference to an object that follows the UITableViewDelegate protocol to each cell, and when creating the cell, assigning that reference to the object that you want to handle that event, when the subview is tapped, send the didSelectRowAtIndexPath message to that reference.
Personally I'd choose the second one, because I'm not a big fan of notifications.

Related

UITextView in UITableViewCell subclass, how to keep the data separate from the view

So I don't know what the best way to follow MVC is. Similar to the address books app, I want to have a UITableVeiewcell that has the ability to edit notes. I figured I would do that with a UITextView in a UITableViewCell subclass. My subclass has just that as a property, and a label that says "notes". I can see a few use cases that I need to consider,
1) when they are done editing and click outside or hit return.
2) when the text goes beyond the size of the cell I need to resize the cell.
Because my UITextView is in IB, is there a good way to define the delegate methods for the UITextView since my UITableView is in another ViewController subclass? Like how do I pass that information back?
Or, is it better to create my UITableViewCell subclass in code since it's just a couple of items so all my delegate and resizing code is done in the view controller class?
Thanks!
After text field editing was finished, you can store it's value in some dictionary in your controller. You can use cell's indexPath as key in this dictionary. In such way you will not lose your data with dequeue cells.
To resize cell you must call reloadData method and change rowHeight property of entire tableView or implement tableView:heightForRowAtIndexPath: delegate method to set needed row height to current cell.
I haven't use UIKit since iOS 3.1, so the second part of my answer can be out of date, but I hope it will help you =)

custom uitablviewcell does not call didSelectRowAtIndexPath in the controller

I have a custom UITableViewCell completely written in code (no IB), it has an accessory button that simply calls didSelectRowAtIndexPath on the table view, and it works correctly and the method is called without problems.
However, when I tap on the cell itself (not on the accessory view) nothing being called, why ?
EDIT: the code is huge to put here ... however, the custom cell contains a ton of labels, couple images and scroll view ...
This is a shot in the dark, but if each cell has many different objects on it (i.e. images, labels, etc) then it may not be working because those objects are what the user is hitting when they try to click a cell. Does the cell turn blue (indicate selection) at all? If not, try hiding/removing those objects for now and see if it works.
If that is the case, then what you may want to do is create an invisible cell or button that sits on top of the other objects and calls didSelectRowAtIndexPath from behind the scenes.
This should solve your problem:
Raise selection event (didSelectRowAtIndexPath) when subview of UITableViewCell is tapped
Try setting your view's userInteractionEnabled property to NO.
This will make it ignore all touch events, and then the views under it will be able to catch these events. - Felipe Sabino
I'd partially answer my question: the wide scroll view is preventing the cell from calling didSelectRowAtIndexPath, removing the scrollView will solve the problem, however, I want to call this method with the existence of the scrollView ... anyone got ideas would be highly appreciated ...
You must post your code to understand what have you done...You have to check out this example to understand whether your code is correct or not...
http://www.edumobile.org/iphone/iphone-programming-tutorials/impliment-a-custom-accessory-view-for-your-uitableview-in-iphone/

backgroundView for a UITableViewCell that is being reordered?

I'm trying to change the background view for cells that the user starts to drag with the handle out on the right hand side.
Apparently the selectedBackground view is not used in this state, is there any other solution?
This seems to be a tough one. It doesn't seem as if Apple has provided a way to notify the developer that the user has started reordering a table view cell. So I think this is what you'd have to do:
Subclass UITableViewCell.
Override the touchesBegan et al methods (this might get you started: http://devblog.wm-innovations.com/2010/03/30/custom-swipe-uitableviewcell/)
If you detect a "drag" touch event and the cell's showsReorderControl property (see http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html) returns YES, swap out the backgroundView, remembering the previous one.
When the touch event ends, restore the old view.
Realize that it's not as easy as above 4 steps.
Custom touch handling inside a tableview is possible but hard. You might have to temporarily disable touch events in the UITableView (climbing up the responder chain) to make this work.
I hope these pointers get you moving in the right direction, though.

UITableViewCell Swipe for Drawer

This is really more of a curiosity than a hard coding question.
Both Facebook and Twitter both have a feature where swiping a UITableViewCell animates the cell off the side to reveal a drawer with more controls underneath. How is something like that accomplished?
Here is a great open-source method for doing exactly this, based on the behavior of the Twitter app:
https://github.com/thermogl/TISwipeableTableView
This is a problem I have tried a couple of different solutions to. I really liked the behavior of Mailbox (mailboxapp.com). So I set out to achieve this. In the end I ended up with what I believe is a very simple solution: use a UIScrollView inside your cell. I have blog post that discusses and a sample app that demonstrates this behavior.
2 ways to detect swipt action
look at the willTransitionToState: method of UITableViewCell.
this method will be invoked when you swipe at the cell.
Custom swipe detection in a TableViewCell
and then you can change your cell view easily.
You could just implement -tableView:willBeginEditingRowAtIndexPath: in your table view delegate.
From the doc,
This method is called when the user swipes horizontally across a row; ... This method gives the delegate an opportunity to adjust the application's user interface to editing mode.
As a UITableViewCell is just a UIView, you can use this fact to basically do anything you like with it.
To solve your problem, I'd attach a UISwipeGestureRecognizer to detect the swipe and then animate the view to a different state.
For example, you could create a custom cell that has it's content view laying above the "actions view". Whenever there is a swipe, you use a UIView animation to move the content view aside and show the action view with a couple of buttons instead. In a custom UITableViewCell you could add a delegate protocol to have the pressed action and the cell being sent to the delegate, i.e. your controller. There you'd trigger what ever there is to trigger and then transition the cell out of the state.

touchesBegan for UITable's Cell

How can I use touchesbegan in a table's cell without having to subclass a whole cell. Something like addTarget..... which is available for a UIButton?
(in vb.net this would be like AddHandler I think)
You must subclass UITableViewCell in order to have access to individual touches on a tableview cell.
You cant do it, you will have to subclass UITableView cell just like the post before me said, then you can add some view in the cells content view there which can delegate the touches to the tableviewcontroller or wherever they need to go...
#Jaco Relkin, you can implement the messages on the UITableViewDelegateProtocol, for example: the message tableView:didSelectRowAtIndexPath: documented here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:didSelectRowAtIndexPath: