I need some guidance on creating a UITableViewCell
that has an image on the left which can be toggled. The image should be tappable and act as a toggle (checkbox).
My parts I'm struggling with are:
- How do I detect taps on the image and handle those differently to
didSelectRowAtIndexPath
? - How do I change the image without performing a
[tableView reloadData]
?
Here's an implementation of the "override touchesBegan:" approach I'm using that is simple and seems to work well.
Just include this class in your project and create and configure a
TouchIconTableViewCell
instead of aUITableView
cell in yourtableView:cellForRowAtIndexPath:
method.TouchIconTableViewCell.h:
TouchIconTableViewCell.m:
Each time you create or re-use the cell, set the
touchIconDelegate
andtouchIconIndexPath
properties. When your icon is touched, the delegate will be invoked. Then you can update the icon or whatever.So the "..obviously need to massage some stuff.." comment means "...this code doesn't work...".
So
should be
As
- (void) viewDidLoad
never gets called.It's actually pretty easy.
Just create a new subclass of UIControl and put it all in there (no need for a separate controller.) Let's call it ToggleImageControl.
Create a ToggleImageControl for each cell, and add it at the appropriate position.
Add a UIImageView to contain the image. Add a target for the touch event.
Set the UIImageView's image property to update the image; that will trigger the redraw without side-effects.
You will obviously need to massage some stuff. You probably want to pass in the two image names to make it more reusable, and also I'd recommend specifying the notification name string from outside the object as well (assuming you are using the notification method.)
It seems that Apple uploaded "TableMultiSelect" as sample codes on iOS Developer Program since 2011-10-12.
Multiple selection in edit mode can be enabled by this code.
http://developer.apple.com/library/ios/#samplecode/TableMultiSelect/Introduction/Intro.html
Though it can be used only from iOS5.
Several hours I could not find this sample code in Stack Overflow, so I added this info to this post.
There's an even EASIER way to do this, if you override touchesBegan: you need to do an if statement to decide if it's within the check marks proximity, if it's not call
[super touchesBegan:touches withEvent:event]
and it will act as though it was selected.I do something similar (starring a favorite) like this, but I think you're demanding a lot from the thumbs of iPhone users with the cell directing people to another view. First of all, I would check out the detail disclosure option on cells. One of the options is a pre-made arrow button that you can attach to. Your call.
You might be able to get away with catching the didSelectRowAtIndexPath event and then doing some other logic instead of redirecting if the touch was on your checkbox, although I don't know how you would get the position. This means you might need to find a way to get ahold of the touch event before it calls didSelectRowAtIndex path, which I'm not quite sure how to do. Have you worked with handling touchesBegan and the like yet?