views:

715

answers:

2

I have a table view controller with multiple UISwitch controls in them. I set the delegate to the table view controller with the same action for all switches. I need to be able to determine what switch was changed, so I create an array of strings that contains the name of each switch. The indexes in the array will be put in the tag property of each UISwitch.

However, I'm ready using the tag property for something else, namely to find the right control in the cell in cellForRowAtIndexPath with viewWithTag! (There are several things I need to set within each cell.)

So, am I thinking along the right lines here? I feel I'm rather limited in how I find out exactly which UISwitch changed its value, so I can do something useful with it.

A: 

You are close in your approach. What I have done in similar situations is create separate UITableViewCell subclasses, set the tag of the UISwitch to be the index.row of the index path, and only use that UITableViewCell subclass in a specific section of the table view. This allows you to use the tag of the cell to uniquely determine what cell has the event without maintaining a separate index list (as it sounds like you are doing).

Because the cell type is unique, you can than easily access the other elements of the cell by creating methods/properties on the UITableViewCell Subclass.

For example:

@interface TableViewToggleCell : UITableViewCell {
    IBOutlet UILabel *toggleNameLabel;
    IBOutlet UILabel *detailedTextLabel;
    IBOutlet UISwitch *toggle;
    NSNumber *value;
    id owner;
}

@property (nonatomic, retain) UILabel *toggleNameLabel;
@property (nonatomic, retain) UILabel *detailedTextLabel;
@property (nonatomic, retain) UISwitch *toggle;
@property (nonatomic, retain) id owner;

-(void) setLable:(NSString*)aString;
-(void) setValue:(NSNumber*)aNum;
-(NSNumber*)value;
-(void) setTagOnToggle:(NSInteger)aTag;

-(IBAction)toggleValue:(id)sender;

@end

In:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ... prior iniitalization code for creating cell is assumed
toggleCell.owner = self;
[toggleCell setLable:@"some string value"];
[toggleCell setTagOnToggle:indexPath.row];
toggleCell.owner = self;
return toggleCell;
   //...  handle cell set up for other cell types as needed
}

Owner is the delegate for the cell and can then be used to initiate actions in your controller. Make sure you connect your UISwitch to the toggleValue Action, so that you can initiate actions in the delegate when the UISwitch changes state:

-(IBAction)toggleValue:(id)sender;
{
    BOOL oldValue = [value boolValue];
    [value release];
    value = [[NSNumber numberWithBool:!oldValue] retain];
    [owner performSelector:@selector(someAction:) withObject:toggle];
}

By passing the UISwitch with the method call, you can then access the index path for the cell. You could also bypass the use of the tag property by explicitly having an ivar to store the NSIndexPath of the cell and then passing the whole cell with the method call.

Chip Coons
How about subclassing the UISwitch and adding a string identifier to it? Would that be bad design?
Thaurin
Hmmm, one problem with your approach is that I'm not using IB, so I add all the controls to the cell's contentView myself. Then, when I need them again in the table controller to set their values for the row being showed, I get them back with viewWithTag. That's pretty much the problem, because I'm already using tag to identify what switch was changed.With a NIB, you just connect some outlets.Did I miss something vital here? I'm gonna try subclassing UISwitch now, although I'm not sure if UISwitch was built to ever be subclassed.
Thaurin
I would recommend diving into IB and subclassing the whole UITableViewCell rather than just UISwitch. It will (I believe) result in more maintainable code over the long haul. Also, once you create one subclass via IB/XCode, it really becomes a snap.
Chip Coons
A: 

I fixed this by subclassing UISwitch like so:

@interface NamedUISwitch : UISwitch {
NSString *name;

}

If anyone sees a problem in subclassing UISwitch this way, speak now or forever be silent. It works, it seems elegant (no index arrays required) and the tag property is free to do whatever it wants.

I read that you have to be careful with subclassing in Objective-C, though...

Thaurin