0

I got a simple question :

In a tableView, i'm displaying a list of customCells. These cells contain labels, SegmentedControllers....

The labels, segmentedController and other cells attributes are declared in a specific class.

How could I, in the class where the list it updated, detect if the segmented controller of the cells are modified? In this class, when I do something like :

if (cell.segmentedControl == 1) { DO MY THINGS }

...nothing ever happens.

Has anybody an advice? :-)

tvincent
  • 73
  • 3
  • 11

1 Answers1

2

First of all, I'm pretty sure you need to use

(cell.segmentedControl.selectedSegmentIndex == 1)

with regards to knowing when it's been changed, use an IBAction connected to your cell class, set up a delegate on your view controller that will get a call back from the cell class when a cell control is clicked.

EDIT -----------------------
In your cell's class .h add

@property (nonatomic, weak) id<NSObject> delegate;

In the cell's class .m

@synthesize delegate = _delegate;

- (IBAction)segmentControlChanged
{
    if ([self.delegate respondsToSelector:@selector(segmentChanged:)]) {
        [self.delegate performSelector:@selector(segmentChanged:) withObject:self];
    }
}

In your ViewController that has the table, in the cellForRowAtIndexPath method, add

cellname.delegate = self

and add the method

-(void)segmentChanged {
  // Put code here to refresh your data source
[self.tableView reloadData];
}
Darren
  • 10,182
  • 20
  • 95
  • 162
  • I meant...I forgot it here...Not in my code ;-) So basically, what I already did : set an IBAction on the segmented controller, so my cell can see when the value is changed. What I have to do : set up a delegate for the cell (but where? In the view controller of the cell?) then use this delegate in my tableView? – tvincent Jul 23 '12 at 12:34
  • Ah sorry. I misunderstood. Yes, add a delegate var to the cell class and in your cellForRowAtIndexPath set cell.delegate=self then your cell can send a message back to your view controller. That in turn can message your data class if needed. – Darren Jul 23 '12 at 13:00
  • This question is 3 years old! – Darren Sep 24 '15 at 09:58