3

我有一个 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;
}

任何阻止细胞复制的帮助将不胜感激。

4

7 回答 7

1
- (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 中有元素:如果它有元素,那么只有循环来到这里。希望对你有帮助

于 2013-10-30T10:19:25.150 回答
0

在你“在这里填写所有标签”的地方,我不确定你是否想要实现什么。

如果在您的自定义单元格中有很多 UILabel 要使用 填充sortedItemsArray,那么在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 您不应该返回[sortedItemsArray count].

如果您的自定义单元格中有一个 UILabel 并且您尝试使用sortedItemsArray基于索引来填充标签,那么您需要做的就是

cell.yourCustomLabel = sortedItemsArray[indexPath.row];

如果您的情况不是上述情况,请附上更多代码进行详细说明。

于 2013-10-30T10:20:10.540 回答
0
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];
    }

我为每个单元格使用唯一标识符,因此没有机会重复/覆盖数据单元格问题。就像滚动后单元格的先前状态已更改,此代码不会发生

于 2014-05-22T07:27:06.790 回答
0

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;
}
于 2013-10-30T13:35:48.163 回答
0

如果您有子类 uitableview 单元格,请确保您在自定义单元格 xib 的属性检查器中提供了“CellIdentifier”。并像这样在 indexpath 处更改您的单元格

静态 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];

}
于 2013-10-30T12:10:17.857 回答
0

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

于 2013-10-30T12:35:17.090 回答
0
NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
于 2016-11-29T11:02:03.440 回答