0

I have multiple sections in table view and a switch for every row. In each section when user clicks on save button, i want to know that which switches are in "ON" or "OFF" state. How to assign tags for switches ?

Jageen
  • 6,345
  • 2
  • 37
  • 56
  • What have you tried ? Assigning a tag likely won't work or at least isn't the best solution for this problem. What are you trying to achieve ? – CW0007007 Feb 12 '15 at 07:39
  • IMHO, you shouldn't be using tags for your switches. I'd recommend you identify which `UITableViewCell` the switch is in (e.g. the `superview` will presumably be the content view, and its superview will be the `UITableViewCell`), and then call `indexPathForCell` to identify which `NSIndexPath` this is for, and go from there. – Rob Feb 12 '15 at 07:42
  • i want to know which switch is in "On " position for a particular row in particular section. – user3816512 Feb 12 '15 at 07:42
  • @user3816512 That's backwards. You don't want to look at the switch to identify whether its on or off. You have a model that indicates whether its on or off, and when the user flips the switch, you update the model. The only time you look at the switch state is when you're responding to a user changing of the switch state, and then it's only to update your model. But if you want to look up the state of a row in the table view, don't look at the cell, but rather refer to your model. Cells are reused and you'll run into problems if you try to use the cell's switch to hold the correct state. – Rob Feb 12 '15 at 07:44
  • possible duplicate of [How can I get index path of cell on switch change event in section based table view](http://stackoverflow.com/questions/28319611/how-can-i-get-index-path-of-cell-on-switch-change-event-in-section-based-table-v) – Jageen Feb 12 '15 at 08:07

2 Answers2

0

If your problem is to give unique tag for each switch in different section then you can do is:

Assign each section a starting tag number like for First Section 1001, so the first cell switch will have tag like switch.tag = 1001+indexPath.row and continues.

For Second Section initial tag will be 2002, so the second cell switch will have tag like switch.tag = 2001+indexPath.row and continues.

So this will solve your tagging problem.

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
0

Implement a delegate for your cell:

@protocol SampleTableViewCellDelegate <NSObject>
- (void)sampleCellDidSelectSwitch:(UISwitch*)sender withIndexPath:(NSIndexPath*)indexPath;
@end

@interface SampleTableViewCell : UITableViewCell

@property (nonatomic, strong) NSIndexPath *cellIndexPath;
@property (nonatomic, assign) id <SampleTableViewCellDelegate> cellDelegate;

@end

In your Cell.m :

@implementation SampleTableViewCell

- (IBAction)switchValueChanged:(UISwitch *)sender {
    if (self.cellDelegate) {
        [self.cellDelegate sampleCellDidSelectSwitch:sender withIndexPath:self.cellIndexPath];
    }
}

@end

In your ViewController.m:

#import "SampleTableViewCell.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate, SampleTableViewCellDelegate>

@end

@implementation ViewController

#pragma mark UITableViewDataSource methods

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"sampleCell";
    SampleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[SampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.cellDelegate = self;
    cell.cellIndexPath = indexPath;
    return cell;
}

#pragma mark - SampleTableViewCellDelegate methods

- (void)sampleCellDidSelectSwitch:(UISwitch*)sender withIndexPath:(NSIndexPath *)indexPath {
    if (sender.on) {

    }
}

@end
iOSfleer
  • 408
  • 6
  • 20