Getting an UISwitch in a table view cell to send an action message
Getting an UISwitch in a table view cell to send an action message
- Subject: Getting an UISwitch in a table view cell to send an action message
- From: WT <email@hidden>
- Date: Sun, 12 Apr 2009 13:24:03 +0200
Hello list,
The iPhone app I'm working on has an in-app settings manager, wich is
just a table view showing the various settings the user can change,
and they're all booleans, so each row has a UISwitch instance as the
cell's accessory view.
In order to treat equally (a) tapping the switch itself or (b) the row
in which it resides, I have the -tableView:didSelectRowAtIndexPath:
method flip the state of the switch and have the switch itself
propagate the change by means of an action message sent to the object
that handles all the settings. (By the way, I have a separate
singleton object to handle the settings so as to keep the settings
management code out of the table view controller class).
When running the app, tapping a row does flip the corresponding switch
(and prints a log message) and tapping the switch itself does flip its
state, but in neither case the action message is being sent.
I've been staring at the code below but can't see anything wrong with
it, so I'd appreciate if anyone could shed some light into why this
isn't working.
Thanks in advance.
Wagner
======
This is in my table view controller class (PrefsViewController):
- (UITableViewCell*) tableView: (UITableView*) tableView
cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
Settings* settings = [Settings singleton];
static NSString* cellID = @"default cell";
UITableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier: cellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero
reuseIdentifier: cellID] autorelease];
}
cell.text = [settings rowTitleForIndexPath: indexPath];
UISwitch* stateSwitch = [[UISwitch alloc] init];
stateSwitch.on = [settings rowStateForIndexPath: indexPath];
stateSwitch.tag = [settings rowTagForIndexPath: indexPath];
// This causes tapping the switch to send an action message to
// 'target'. Since the switch tag is set above, the target can
// extract the sender's section and row values from its tag.
cell.accessoryView = stateSwitch;
cell.accessoryAction = @selector(actionFlipRowState:);
cell.target = settings;
[stateSwitch release];
return cell;
}
- (void) tableView: (UITableView*) tableView
didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
NSLog(@"PrefsViewController: -tableView:didSelectRowAtIndexPath:");
UITableViewCell* cell = [tableView cellForRowAtIndexPath:
indexPath];
UISwitch* stateSwitch = (UISwitch*) cell.accessoryView;
[tableView deselectRowAtIndexPath: indexPath animated: NO];
// This should trigger the switch to send an action message
// to the Settings object.
[stateSwitch setOn: ! stateSwitch.on animated: YES];
}
This is in my settings manager class (Settings):
- (NSUInteger) rowTagForIndexPath: (NSIndexPath*) indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
return kNumSections * row + section;
}
- (void) actionFlipRowState: (id) sender
{
NSLog(@"Settings: -actionFlipRowState:");
UIView* view = (UIView*) sender;
NSUInteger tag = [view tag];
NSUInteger row = tag / kNumSections;
NSUInteger section = tag % kNumSections;
// Flip the boolean state associated with
// the given row and section values.
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden