tags:

views:

806

answers:

4

Hi friends,

Hope that all are fine.

I have 22 UISwitch in a table view and I have created all these switch objects in one function and set them in the table view.

How can I know which switch is on or off? I want to do this through only one action method but I don't know how to do that.

If anyone has faced this problem, please give me ideas...

Thanks, Haresh.

+1  A: 

The best way to do something like this is to set up each table cell with your table view controller as delegate along with a tag indicating which switch that cell is presenting - then have each cell detect the switch change, and in turn tell the delegate that the switch with the indicated ID has been altered. You can keep the switch states in an NSDictionary stored by cell switch tag.

This all of course implies a custom UITableViewCell class so you can store a tag and a delegate link back to your UITableViewController (note, do NOT retain this link back to your table view controller)

EDIT This answer provides a different approach you may prefer:

http://stackoverflow.com/questions/394287/getting-the-row-of-a-nsbuttoncell

Kendall Helmstetter Gelner
A: 

hi,

 thanks for ur reply....

 but when i press on switc object, switch object is working but its no selecting any rows.

 means i have created didSelectRows but its no calling when i press switch. s how can i detect this cell?

Thanks.

A: 

tableView:didSelectRowAtIndexPath: will only fire for a selection of the row itself within your table view, which doesn't sound like what you're trying to do.

In the method where you create your UISlider instances, you could store those instances as keys in an NSMutableDictionary (using setObject:forKey), with NSNumbers as the values for use in a later switch statement. For each UISlider, make sure that the change in their value is being recorded by setting the target to be a method in the controller using something similar to the following:

[slider addTarget:self action:@selector(sliderValueDidChange:forEvent:) forControlEvents:UIControlEventAllTouchEvents];

and set up a method in your controller similar to this:

- (void)sliderValueDidChange:(id)sender forEvent:(UIEvent *)event;

within which you can use the sender, the NSDictionary of UISlider instances, and a switch statement on the NSNumber's intValue to determine which slider's value has changed and what the new value is.

Instead of an on/off switch, may I also suggest the use of a checkmark as an accessory view? Then, you could use tableView:didSelectRowAtIndexPath: to track user selection of the row and display a checkmark in the accessory view if the element has become selected. This might be more in line with Apple's Human Interface Guidelines.

Brad Larson
A: 

checkmark on accessory view?????? found one example 'Accessory' but they have set one button and set the image of checkmark button and uncheckmark button..you mean to say same like that

Your response should be a comment on the above answer, but yes, set the accessoryType of your UITableViewCell to be UITableViewCellAccessoryCheckmark to add the checkmark to the cell.
Brad Larson