3

I have custom cell having switch in few cells at right side. what I want is to store value of specific cell on switch change event. Table view has number sections so I can't set tag for switch because I need section as well as row to obtain index path.

Any suggestion any alternative but I have to use UISwitch in section based table view.

Thanks

Ahad Khan
  • 411
  • 2
  • 18
  • Is it ok if you get that cell object on UISwitch value changes selecter ? – Jageen Feb 04 '15 at 11:02
  • Yes, in UI switch state get changed but I can't decide which switch stat has been changed. – Ahad Khan Feb 04 '15 at 11:14
  • see my answer it may be halp – Jageen Feb 04 '15 at 11:22
  • There are two things you need to deal with. 1) Your cell is a view onto and controller for a piece of your overall data. It needs to contain a reference for that data. 2) You need a handler for when that data is changed. You can do this in the cell or better do it in a central place like the table view. See my answer which shows how. – Rory McKinnel Feb 04 '15 at 11:40

3 Answers3

6

In your custom cell add properties which help you identify the information the cell represents. Index path, indexes for your data model etc...

Then add a block property to the cell which you can call to tell a UITableView or any other piece of code when a cell switch changes. e.g.

@property (nonatomic,copy) void (^onSwitchChange)(UITableViewCell *cell);

Inside your custom cell code, add an action handler for the UISwitch. When it fires, call self.onSwitchChange(self) which will notify the code which registered an onSwitchChange block that a switch has changed and on which cell.

In your table view when you create the cell, set the onSwitchChange block as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
  <snip>

  YourUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:yourCellIdentifier forIndexPath:indexPath];
  cell.onSwitchChange=^(UITableViewCell *cellAffected){
    // Add code to deal with the swicth switch using properties of cellAffected
    ... Your handler code here ...
  }];

  <snip>
}

This lets you handle all the changes in the table view controller. Hope this helps.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
1

The answer from @Jageen works. I had to find out which superview the cell is, mine was one more level higher.

UITableViewCell *cell = (UITableViewCell*)[sender superview].superview.superview;
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
NSLog(@"Section %ld Row : %ld",(long)indexPath.section,(long)indexPath.row); // print row section and index
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
0

You can still create tags even if you have sections if you have some idea of about max rows in a largest section. For example if you think there will be 1000 rows in a section then you can create tag using following formula.

tag = section * 1000 + row;

later in your IBAction of switch you can find out the indexpath (section and row) using following:

section = tag/1000;
row = tag%1000;

If you have no idea of how many rows your section will have you can find out the cell using sender.superview.superview (be careful if have added any other views in hierarchy).

Rory McKinnel's answer is still the cleanest solution for your problem.

Community
  • 1
  • 1