当单元格离开屏幕时,Tableview单元格内容会发生变化

时间:2016-06-10 08:07:32

标签: ios objective-c tableviewcell

我有两种tableview单元格,比如支付和未支付。我左边有复选框,没有付费单元格。 首次加载tableview时,所有单元格的格式都是正确的。 但当我向上滚动时,当细胞离开屏幕并进入屏幕时,它们的内容会发生变化。显示图像。enter image description here

并且在向上和向下滚动之后我得到了这个。

enter image description here

真的很令人沮丧。我搜索了SO,但没有用。

我的代码如下:

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


    data1 = [[responseDict valueForKey:@"cases"]objectAtIndex:indexPath.section];
    data1 = [data1 valueForKey:@"procedures"];
    data1 = [data1 objectAtIndex:indexPath.row];
    //[[[valueForKey:@"paid"]objectAtIndex:indexPath.row]valueForKey:@"procedures"];
    static NSString *CellIdentifier = @"Cell";

        //if ([payemtStr  isEqual:@"0"]) {
        //UnpiadCell



        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Configure the cell...
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }


    UIButton * checkBtn= (UIButton *)[cell viewWithTag:101];
    UILabel *payToLbl = (UILabel *)[cell viewWithTag:100];
    UIButton * expandBtn= (UIButton *)[cell viewWithTag:106];

    if ([[data1 valueForKey:@"paid"]boolValue] ==1) {
        payToLbl.text = @"";
        [checkBtn setHidden:YES];
        [expandBtn setHidden:YES];
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    else {
    payToLbl.text = @"Pay to";
        [checkBtn addTarget:self action:@selector(checkBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [expandBtn addTarget:self action:@selector(collapseExpandButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        if([[[selectedCellsArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row ] isEqualToString:@"Uncheck"])
            [checkBtn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
        else
            [checkBtn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
        // [[expandCellsArray objectAtIndex:indexPath.row] isEqualToString:@"Expand"]
        if([[[expandCellsArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row ] isEqualToString:@"Expand"])
            [expandBtn setImage:[UIImage imageNamed:@"collapse.png"] forState:UIControlStateNormal];
        else
            [expandBtn setImage:[UIImage imageNamed:@"expand.png"] forState:UIControlStateNormal];

//        if ([selectedCellsArray containsObject:@"Check"]) {
//            self.payBtn.hidden = NO;
//        }
//        else {
//            self.payBtn.hidden = YES;
//        }

    }


    UILabel *mainTitleLbk = (UILabel *)[cell viewWithTag:102];
    mainTitleLbk.textColor= [UIColor blackColor];
    mainTitleLbk.text = [data1 valueForKey:@"provider"];
    ((UILabel *)[cell viewWithTag:103]).text = [data1 valueForKey:@"description"];
    ((UILabel *)[cell viewWithTag:104]).text = [NSString stringWithFormat:@"%@ %@ ",
                                                [data1 valueForKey:@"line_1"],
                                                [data1 valueForKey:@"line_2"]
                                                ];
    ((UILabel *)[cell viewWithTag:109]).text = [NSString stringWithFormat:@"%@, %@", [data1 valueForKey:@"city"],[data1 valueForKey:@"state"]];

    // [data1 valueForKey:@"zip"]
    ((UILabel *)[cell viewWithTag:105]).text = [NSString stringWithFormat:@"%@ ,%@" ,[data1 valueForKey:@"zip"],[data1 valueForKey:@"phone"]];
    // handeling check uncheck buttons
    return cell;


}

1 个答案:

答案 0 :(得分:1)

每次出现新行时都会重复使用UITableViewCell。因此,在向上和向下滚动之后,第一行不是新创建的UITableviewCell。添加2行代码可以解决问题:

{
payToLbl.text = @"Pay to";
[checkBtn setHidden:NO];
[expandBtn setHidden:NO];
.......

请同时清除这些按钮操作。