UITableViewCells change after scrolling down then

2019-07-04 09:47发布

I have a UITableView where when I scroll down and back up , the values are changing in the UILabels of cells. I cannot figure out why this is happening.

I am using custom UITableViewCells that I have subclassed myself.

This is what my code looks like minus entering all of the values into the labels as I think the error is not there but in the way i declare the tableview delegate methods or something like that lol.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (sortedItemsArray == nil) {
        return 0;
    } else
        return [sortedItemsArray count];
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CustomallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    if ([sortedItemsArray count] > 0) {
        currentallDictionary = [sortedItemsArray objectAtIndex:indexPath.row];

        NSNumber *tempDU = [currentallDictionary objectForKey:@"DU"];
        NSInteger myInteger = [tempDU integerValue];

        if (myInteger == 0) {

//            NSLog(@"%@", currentallDictionary);
            //assign vals to labels

            NSString *areaString = [currentallDictionary objectForKey:@"area"];
            if ((NSNull *) areaString != [NSNull null]) {
                cell.areaLabel.text = areaString;
            } else {
                cell.areaLabel.text = @" ";
            }


            NSString *stageString = [currentallDictionary objectForKey:@"stage"];
            if ((NSNull *) stageString != [NSNull null]) {
                cell.stageLabel.text = stageString;
            } else {
                cell.stageLabel.text = @" ";
            }


            NSString *floorLabelString = [currentallDictionary objectForKey:@"floorNo"];
            if ((NSNull *) floorLabelString != [NSNull null]) {
                cell.floorLabel.text = floorLabelString;
            } else  {
                cell.floorLabel.text = @" ";
            }


            NSString *floorDescLabelString = [currentallDictionary objectForKey:@"floorDesc"];
            if ((NSNull *) floorDescLabelString != [NSNull null]) {
                cell.floorDescLabel.text = floorDescLabelString;
            } else  {
                cell.floorDescLabel.text = @" ";
            }



//Buttons
            tDxStateAString = [currentallDictionary objectForKey:@"tDxStateA"];
            tDxStateBString = [currentallDictionary objectForKey:@"tDxStateB"];
            tHasDxString = [currentallDictionary objectForKey:@"tHasDx"];

            if ([tHasDxString isEqualToString:@"T"]) {
                tDxQtyString = [currentallDictionary objectForKey:@"tDxQty"];
                if ((NSNull *) tDxQtyString != [NSNull null]) {
                    cell.quantityALabel.text = tDxQtyString;


                    if (([tDxStateAString isEqualToString:@"T"]) && ([tDxStateBString isEqualToString:@"W"])) {
                        UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
                        [cell.DxfitButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
                    } else if (([tDxStateAString isEqualToString:@"T"]) && ([tDxStateBString isEqualToString:@"R"])) {
                        UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
                        [cell.DxfitButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
                    }else {
                        UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
                        [cell.DxfitButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
                        cell.DxfitButtonImage.userInteractionEnabled = YES;
                    }

                } else {
                    cell.quantityALabel.text = @" ";
                    cell.DxfitButtonImage.userInteractionEnabled = NO;
                }
            }

            tStateAString = [currentallDictionary objectForKey:@"tStateA"];
            tStateBString = [currentallDictionary objectForKey:@"tStateB"];
            tHasInsString = [currentallDictionary objectForKey:@"tHasIns"];
            if ([tHasInsString isEqualToString:@"T"]) {

                tInsQtyString = [currentallDictionary objectForKey:@"tInsQty"];
                if ((NSNull *) tInsQtyString != [NSNull null]) {
                    cell.quantityBLabel.text = tInsQtyString;

                    if (([tStateAString isEqualToString:@"T"]) && ([tStateBString isEqualToString:@"W"])) {
                        UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
                        [cell.allButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
                    } else if (([tStateAString isEqualToString:@"T"]) && ([tStateBString isEqualToString:@"R"])) {
                        UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
                        [cell.allButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
                    } else {
                        UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
                        [cell.allButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
                    }
                } else {
                    cell.quantityBLabel.text = @" ";
                    cell.allButtonImage.userInteractionEnabled = NO;
                }
            }



        }
    }
    return cell;
}

any help stoping the repition of cells would be greatly appreciated.

7条回答
啃猪蹄的小仙女
2楼-- · 2019-07-04 10:26

if you have subclassed uitableview cell make sure you gave the "CellIdentifier" inside the atttrib utes inspector of your Custom cell xib. and change your cell for row at indexpath like this

static NSString * cellIdentifier = @"cell";

CustomInstallCell * cell = (CustomInstallCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomInstallCell" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];

}
查看更多
家丑人穷心不美
3楼-- · 2019-07-04 10:27

UITableViewCell reuse cell when scroll table view,cells are reused based on CellIdentifier,in table view we create the new views only cell==nil other wise we reuse cell in table view,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomInstallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CustomInstallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if ([sortedItemsArray count] > 0) {

          UIView *view=[CustomInstallCell viewWithTag:your view tag];
      //view is sub view of CustomInstallCell

        }
    }
    return cell;
}
查看更多
家丑人穷心不美
4楼-- · 2019-07-04 10:29

UITableViewController work with reusable cells. The controller creates a limited number of cells and scrolls simply overwrites the contents. This can lead to duplication of certain properties in the cells. To avoid duplication, you can use the method tableView:willDisplayCell:forRowAtIndexPath: of UITableViewDelegate. In this method, check out what is duplicated or incorrect and set it correctly.

查看更多
Anthone
5楼-- · 2019-07-04 10:29
static NSString *CellIdentifier;        
    if(IPAD)
        CellIdentifier=[NSString stringWithFormat:@"EditCell_ipad%d",indexPath.row];
    else
        CellIdentifier=[NSString stringWithFormat:@"EditCell_iphone%d",indexPath.row];


     CustomallCell *cell=(CustomallCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomallCell_TablviewCell" owner:self options:nil];
        if(IPAD)
        cell = (CustomallCell*)[nib objectAtIndex:0];
        else
        cell = (CustomallCell *)[nib objectAtIndex:1];
    }

I am using unique Idetifier for each cell, so there is no chance to repeat/overwrite data cell problem.Like after scrolling the previous status of cell is changed that will not happen with this code

查看更多
Viruses.
6楼-- · 2019-07-04 10:29
NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
查看更多
闹够了就滚
7楼-- · 2019-07-04 10:34

At the place you "fill in all the labels here", I'm not sure if what do you want to achieve.

If in your custom cell you have a lot of UILabel to fill using sortedItemsArray, then in the - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section you should not return the [sortedItemsArray count].

If there's one UILabel in your custom cell and you try to fill the label using sortedItemsArray based on the index, then all you need to do is

cell.yourCustomLabel = sortedItemsArray[indexPath.row];

If your case is none of above, please attach more code to elaborate.

查看更多
登录 后发表回答