I have designed a custom cell in the Interface Builder.
This is a reusable cell of a e-commerce cart app.The problem lies with the UIStepper.I can have any number of these cells based on the number of items user adds to the cart.So the issue is that if I increment stepper values of cell 1,2 and 3 The value from cell 4 will start from some random number and not one.Currently I read values from a .plist file and I created a product model and saved those to the model and created a product models array.The data is displayed from the array.I have created a delegate for getting the values from custom cell to the View Controller.The stepper value change function is shown below.
- (IBAction)valueChangedAction:(UIStepper *)sender {
NSLog(@"sender.value ---------- %f",sender.value);
self.stepperCount = sender.value;
self.cartItemQuantityLabel.text=[NSString stringWithFormat:@"%ld",(long)self.stepperCount];
[self.delegate valueDidChangeStepper:self.stepperCount cellRow:self.cellRow];
}
The implementation of the function in View Controller is as below.
-(void)valueDidChangeStepper:(NSInteger)valueofStepper cellRow:(NSInteger)cellRow
{
ProductModel *productModel=[productsArray objectAtIndex:cellRow];
productModel.cartItemQuantity=[NSString stringWithFormat:@"%d",valueofStepper];
NSLog(@"stepper value %d from row %d",valueofStepper,cellRow);
}
The value is updated in the Product Model. For ex. If I select Qty 8 in cell 1,Qty 5 in cell 2 ,Qty 7 in cell 3, If I increment a value in cell 4,It jumps straight from 1 to some random value like 10 or so. EDIT:the cellForRowAtIndexPath method is as follows
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cartItemCellIdentifier =@"CartItemCell";
CartItemCell *cartItemCell =[self.cartTableView dequeueReusableCellWithIdentifier:cartItemCellIdentifier];
if(itemsArray)
{
ProductModel *productFromArray=[productsArray objectAtIndex:indexPath.row];
cartItemCell.cartItemNameLabel.text=productFromArray.cartItemName;
cartItemCell.cartItemQuantityLabel.text=productFromArray.cartItemQuantity;
cartItemCell.cartItemCostLabel.text=productFromArray.cartItemCost;
cartItemCell.cartItemImage.image=[UIImage imageNamed:productFromArray.cartItemImage];
cartItemCell.cellRow=indexPath.row;
cartItemCell.delegate=self;
}
return cartItemCell;
}
I set the cellrow as indexpath.row in the method itself.So I get the which stepper was clicked.But still I get random values.