Logic
Logic

Reputation: 705

Tableview cell content is changed when cell goes off screen

I have two kinds of tableview cells , say Paid and unpaid.I have check boxes on left , to unpaid cells. When tableview is first time loaded , all cells are in correct format. but when i scroll up , as the cells go off screen and come bak screen , their content is changed.showing the images.enter image description here

and after scrolling up and down i get this.

enter image description here

it is really frustrating. i have searched over SO , but of no avail.

My code is below :

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


}

Upvotes: 0

Views: 86

Answers (1)

Son Le
Son Le

Reputation: 154

UITableViewCell is reused everytime a new row appear. So after scrolling up and down, the first row is not the newly created UITableviewCell. Adding 2 line of code may fix the problem:

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

Please also clear these button actions.

Upvotes: 1

Related Questions