One IBAction for multiple custom prototype cells with buttons

Using storyboard, I have a table view controller containing multiple dynamic prototype cells. One of the cells is a custom dynamic prototype cell (for which I created a subclass) containing a label and a switch. I have the action for the switch wired to an action method (say switchChanged:) in the view controller. In cellForRowAtIndexPath, I configure and return the cell appropriate for the specified index. So far so good.

The problem: my application has multiple instances of these custom cells, so how do I differentiate which switch has changed? One thought is that in the view controller I can create a member switch for each cell and link them to a specific cell switch when cellForRowAtIndexPath is called for the first time. Then I can use these member switches to compare with the switch that is passed into switchChanged:.

I know that I can try it and get an immediate answer, but I was hoping for a discussion of how other developers do what I am trying to do. Is this the best/worst/ok approach and what are practical alternatives?




I had this situation once (not with switches, but I believe it applies just the same). I've managed to get around it by subclassing the object class and adding the required properties/methods inside the subclass.

When calling the action, your sender will be the subclass, and you can access your added code there.

I don't know if it is the case, but if you're only trying to change a value, you should use bind the switch value to the property when creating the object. It will not even need an IBAction to call.

EDIT: Example:

@interface MySwitch : UISwitch
@property (nonatomic, assign) NSUInteger someProperty;

Then, every time you create a cell, you can set "someProperty" to anything you want.

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
  // yada yada yada...
  UITableViewCell *cell;
  // yada yada yada...

  [cell.myLinkedSwitch  setSomeProperty:indexPath.row];

Unless you're not creating your cells using the tableView:cellForRowAtIndexPath: method. Then you probably should use bindings to get your value to the right place.

Instead of adding a separate subclass, I just stored the row in each button Disabled Title property. This worked very will with little effort. This first code is in the CellForRowAtIndexPath:

NSString *strRow = [[NSString alloc] initWithFormat:@"%i",useRow];
[btnPreferredChk setTitle:strRow forState:UIControlStateDisabled];

Then my action method for the button uses that value to perform the appropriate activity.

- (IBAction)goStorePick:(id)sender
  UIButton *useButton = [[UIButton alloc] init];
  useButton = sender;
  NSInteger *storeRow = [[useButton titleForState:UIControlStateDisabled] integerValue];

  NSString *CMIMsg = [[NSString alloc] initWithFormat:@"goStorePick Method Executed at Row: %i", storeRow];

  [self shwMessage:CMIMsg];

This worked well for me.

Need Your Help

C# getting value from app.config


Is it possible to read the setting value URL using ConfigurationManager ?

Can you perform aggregations or joins with falcor?


Often a client needs aggregations or joins over the data. How would you handle that in falcor? I don't see any notion of transform or join in the docs. Perhaps with call? It is introduced as a way of