Hello, I'm trying to implement UISegmentedControl in a custom cell in order to handle people's answer to some test.
So here is how this cell looks like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"testCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"testCell" owner:self options:nil];
cell = testCell;
self.testCell = nil;
}
VerticallyAlignedLabel *cellLabel; cellLabel = (VerticallyAlignedLabel *)[cell viewWithTag:11]; cellLabel.font = [UIFont systemFontOfSize:13]; cellLabel.text = [array objectAtIndex:indexPath.section]; cellControl = (UISegmentedControl *)[cell viewWithTag:22]; cellControl.momentary = NO; [cellControl addTarget:self action:@selector(segmentValuePicked:) forControlEvents:UIControlEventValueChanged]; [segmentValueArray insertObject:segmentValue atIndex:indexPath.section]; return cell; }
It is a Label and beyond it a segmented control with 6 buttons
So what I need is to remember which button user pushed and assign one integer per one cell depending on people's choice. For example, if user touches button 1 - the integer segment Value would be 0, if button 2 - segmentValue = 2 and so on - very simple. This is happening in method segmentValuePicked:
-(int) segmentValuePicked:(UISegmentedControl *)sender {
NSInteger test = [sender selectedSegmentIndex];
segmentValue = 0; switch (test) { case 0: segmentValue = 0; return segmentValue; break; case 1: segmentValue = 2; return segmentValue; break; case 2: segmentValue = 4; return segmentValue; break; case 3: segmentValue = 6; return segmentValue; break; case 4: segmentValue = 8; return segmentValue; break; case 5: segmentValue = 10; return segmentValue; break; } return 0; }
So in my oppinion this is what this code should do - handle people's choice and remember it in an array of integers to calcualate test results later
[cellControl addTarget:self
action:@selector(segmentValuePicked:)
forControlEvents:UIControlEventValueChanged];
[segmentValueArray insertObject:segmentValue atIndex:indexPath.section];
However what I got every time is runtime error -
2010-09-05 15:19:13.925 HR[1879:207] -[UISegmentedControl segmentValuePicked]: unrecognized selector sent to instance 0x5d17850
2010-09-05 15:19:13.928 HR[1879:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISegmentedControl segmentValuePicked]: unrecognized selector sent to instance 0x5d17850'
anybody can guess why?
maybe cellForRowAtIndexPath: isn't right place to do this, but where else?