简体   繁体   中英

label value in custom tableview cell is changing while scrolling

Label value in custom tableview cell is changing while scrolling the tableview

 static NSString *cellIdentifier = @"Cell";
    cell = (CheckOutCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // If there is no cell to reuse, create a new one
    if(cell == nil)
    {
        cell = [[CheckOutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];



    }

    cell.delegate = self;

    CheckOutGroup * checkOut=_cartArray[indexPath.row];
    cell.name.text=checkOut.name;
    cell.size.text=checkOut.size;
    cell.priceValue=checkOut.price ;
    [array addObject:checkOut.dictionary];
    [itemIdArray addObject:checkOut.itemId];
    cell.price.text=[NSString stringWithFormat:@"Price:%@%@",self.currValue,checkOut.price];

    cell.quantity.text=[NSString stringWithFormat:@"Qty:%d",1];
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sqwip.ignivainfotech.net/%@",checkOut.image]]];
        cell.checkOutImage.image=[UIImage imageWithData:imgData];
    NSUserDefaults *storeDefaults = [NSUserDefaults standardUserDefaults];
    [storeDefaults setObject:self.currValue forKey:@"cur"];

    cell.increaseBtn.tag=indexPath.row;
    val=[checkOut.price intValue];

    if (check==0) {
        check=check+[checkOut.price intValue];
    }
    else
    check=check+[checkOut.price intValue];


    self.totalPrice.text=[NSString stringWithFormat:@"TotalPrice:%@%d",self.currValue,check];


    return cell;

}

When a cell needs to be displayed (IE scrolling back onto the screen) the tableview controller is going to look at the data source and fire the some of its methods so it can display the correct information. In short its going to recall this method every time a cell needs to be displayed:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

If the cell is changing its because the data source is changing! I suggest placing a break point inside cellForRow and make sure the data from your:

CheckOutGroup * checkOut=_cartArray[indexPath.row];

or whatever is correct for the right cell before and after scrolling. If it is different and it shouldn't be, possibly you are grabbing the wrong data from the URL on the next pass or your indexes are getting out of place.

It's because of re-useability. Since UITableView reuse its cell on scrolling. To resolve this just make your cell nil.
Try this on CellForRowAtIndexPath:-

    static NSString *cellIdentifier = @"Cell";
    cell = (CheckOutCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell)
       cell = nil
    // If there is no cell to reuse, create a new one
    if(cell == nil)
    {
        cell = [[CheckOutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM