0

I have designed a custom cell in the Interface Builder.enter image description here

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.

Haseeb Mohamed
  • 1,459
  • 1
  • 11
  • 15
  • Don't pass `cellRow` to your delegate method, pass the cell (ie `self`) then use `indexPathForCell` in your delegate method to determine which row was affected – Paulw11 Aug 30 '16 at 10:21

2 Answers2

0

table view or collection view deque the cell, and reuse cell instead of creating new one for memory efficiency. So, you have to play with cellForRowAtIndexpath method to manage tableview datasource. From forth row you are getting random data thats mean your forth cell is displayed by using previous cells.

So, in your case you have to store value of stepper as per indexPath or row and have to manage it cellforrowatindexpath.

dequeueReusableCellWithIdentifier, refer apple documentation for more details.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

You almost certainly aren't fully configuring your cells in cellForRowAtIndexPath, and are picking up stale values from recycled cells. Are you setting the stepper value to zero in all cases?

Post your code for cellForRowAtIndexPath.

And, as Paulw11 pointed out in his comment, an instance variable cellRow can't have the correct cell row no matter which cell's stepper you tap. You need to use the origin of the stepper to figure out which cell it's in. (See Chris' answer to this question for info on figuring out the cell's indexPath for a button or other control: Detecting which UIButton was pressed in a UITableView)

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272