Dancer
Dancer

Reputation: 17671

UITableViewCell - Receive Stepper instance value

I have setup a a UITableViewController which populates a custom cell with fitness attributes - the brief requires the user to be able to enter a 'actual' value if they have exceeded / missed their taget - I've added a stepper for this purpose - the stepper is connected to the custom cells .h file - which in turn is connected to the uitableviews .m file.

I'm struggling to understand how could I pass the altered value back to the uitableviewcontroller and how would I know which instance has passed the value!?

enter image description here

Upvotes: 0

Views: 571

Answers (2)

Mark Woollard
Mark Woollard

Reputation: 159

Something along these lines...

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
        MyCustomCell* cell = [tableView dequeueReusableCellForIdentifier:MyCustomCellIdentifier];
        // If newly created cell we need to add a target
        if (![[cell.stepperControl allTargets] containsObject:self])
        {
            [cell.stepperControl addTarget:self action:@selector(stepped:) forControlEvents:UIControlEventValueChanged];
        }

        cell.stepperControl.tag = indexPath.row + indexPath.section * 10000;

        // Rest of configuration...

        return cell;
    }

    - (void)stepped:(UIStepper*)stepper
    {
        int row = stepper.tag % 10000;
        int section = stepper.tag / 10000;

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

        // Now you know which row was changed so get the cell
        MyCustomCell *cell = (MyCustomCell*)[self.tableView cellForRowAtIndexPath:indexPath];

        // Read required data from the cell through custom properties...

    }

Upvotes: 3

Vlad Papko
Vlad Papko

Reputation: 13302

You could create subclass of UITableViewController and store steppers actual values in array like this:

@interface CustomTableViewController () 
// Add property for storing steppers' current values
@property (nonatomic, strong) NSMutableArray stepperValues;

@end

@implmentation CustomTableViewController 

- (instancetype)init {
    self = [super init];
    if (self) {
        // Initiate array with default values
        self.stepperValues = [NSMutableArray arrayWithCapacity:numberOFCells];
        for (int i = 0; i < numberOfCells; i++) {
            [self.stepperValues addObject:@(0)];
        }
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Init cell here
    // ...
    // Set last saved value
    cell.stepper.value = [self.stepperValues[indexPath.row] doubleValue];

    // Save stepper's row for retrieving it in valueChanged: method
    cell.stepper.tag = indexPath.row;

    // Add action for handling value changes
    [cell.stepper addTarget:self action:@selector(stepperValueChanged:) forControlEvents:UIControlEventValueChanged];
    return cell;
}

- (void)stepperValueChanged:(UIStepper *)sender {
    // Replace old stepper value with new one
    [self.stepperValues replaceObjectAtIndex:sender.tag withObject:@(sender.value)];
}

@end

By this code stepperValues will contain actual values and you could use it for your aims.

Upvotes: 0

Related Questions