ios UITableViewCells在向下滚动然后向上滚动后更改

r7xajy2e  于 2022-12-05  发布在  iOS
关注(0)|答案(7)|浏览(122)

我有一个UITableView,当我向下滚动和向上滚动时,单元格的UILabels中的值会发生变化。我不知道为什么会发生这种情况。
我使用的是我自己子类化的自定义UITableViewCells。
这就是我的代码看起来的样子,减去将所有值输入到标签中,因为我认为错误不存在,而是在我声明tableview委托方法的方式或类似的东西中。

- (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;
}

我怎样才能停止细胞的重复?

at0kjp5o

at0kjp5o1#

- (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];
UILabel *lbl_name = [[UILabel alloc]initWithFrame:CGRectMake(10, 7, 280, 25)];
            lbl_name.tag = 1000;
            [lbl_name setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
            lbl_name.lineBreakMode = UILineBreakModeWordWrap;
            lbl_name.numberOfLines = 0;
            [lbl_name setBackgroundColor:[UIColor clearColor]];
            [lbl_name sizeToFit];
            [cell.contentView addSubview:lbl_name];
     //Create all your Labels here and set the tag
    }
    //Access your lbl by the tag here
    UILabel *lbl_name = (UILabel *)[cell.contentView viewWithTag:1000];
    [lbl_name setText:[yourArray objectAtIndex:indexPath.row]];

        cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
}

这样试试..那么就不需要检查数组cellForRowAtIndexPath中有元素了:如果它有元素,那么只有循环来这里。我希望这将有助于你

syqv5f0l

syqv5f0l2#

在你“填写所有标签”的地方,我不确定你想达到什么目的。
如果在你的自定义单元格中你有很多UILabel要用sortedItemsArray填充,那么在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section中你不应该返回[sortedItemsArray count]
如果自定义单元格中有一个UILabel,并且您尝试使用sortedItemsArray基于索引填充该标签,那么您需要做的就是
第一步:创建一个新的数组;
如果你的情况不是以上任何一种,请附上更多的代码来详细说明。

j91ykkif

j91ykkif3#

如果你已经子类化了uitableview单元格,确保你在你的自定义单元格xib的属性检查器中给出了“单元格标识符”,并像这样在indexpath处更改你的单元格行
静态NSString * 单元标识符= @“单元”;

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

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

}
jum4pzuy

jum4pzuy4#

UITableViewController使用可重复使用的单元格。该控制器创建有限数量的单元格,滚动只是覆盖内容。这可能导致单元格中某些属性的重复。为了避免重复,可以使用UITableViewDelegate的tableView:willDisplayCell:forRowAtIndexPath:方法。在此方法中,检查重复或不正确的内容,并正确设置。

k10s72fa

k10s72fa5#

UITableViewCell重复使用单元格当滚动表格视图时,单元格基于CellIdentifier重复使用,在表格视图中,我们仅创建新视图cell==nil,否则我们在表格视图中重复使用单元格,

- (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;
}
3okqufwl

3okqufwl6#

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

efzxgjgh

efzxgjgh7#

NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];

相关问题