Emon
Emon

Reputation: 958

Why UITableview data are overlapping?

Why tableview data are overlapping? i have attached my code.

If i explain my code again then

  1. xml data are in records array which are displaying in tableview.
  2. First time when program execute then data are in right place but when i start scroll vertically the data start to display in various cell.suppose first cell shows in last cell,second cell data shows in first cell.....so on.

NOTE THAT: For overlapping problem i also used switch and viewwithtag but still now tableview data start to display in wrong cell when i start scrolling on UITableview.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
   // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSLog(@"%d",indexPath.section);

    switch (indexPath.section) {
        case 0:
            order_cellView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, cell.frame.size.width,cell.frame.size.height) ];
            order_cellView.backgroundColor = [UIColor redColor];
            order_cellView.tag =1000;

            NSLog(@"index path 0");
            CGRect frame;
            frame.origin.x =0;
            frame.origin.y = 10;
            frame.size.height = 30;
            frame.size.width = 230;

            CGRect valueFrame;
            valueFrame.origin.x =237;
            valueFrame.origin.y = 5;
            valueFrame.size.height = 30;
            valueFrame.size.width = 80;


            for(int m=0;m<numberOfProduct;m++){
                NSLog(@"for loop here");
                productLabel = [[UILabel alloc] initWithFrame:frame];
                productLabel.tag = m;
                productLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
                productLabel.backgroundColor = [UIColor clearColor];
                productLabel.lineBreakMode = UILineBreakModeCharacterWrap;
                productLabel.numberOfLines = 2;
                productLabel.textAlignment = UITextAlignmentCenter;
                [order_cellView addSubview:productLabel];
                productLabel.text =  [NSString stringWithFormat:@"%@ x %@ x %@",[[products objectAtIndex:m] objectAtIndex:1],[[products objectAtIndex:m] objectAtIndex:2],[[products objectAtIndex:m] objectAtIndex:3]];
                frame.origin.y += 45;

                productValueLabel = [[UILabel alloc] initWithFrame:valueFrame];
                productValueLabel.tag = m+2;
                productValueLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
                productValueLabel.backgroundColor = [UIColor clearColor];
                productValueLabel.lineBreakMode = UILineBreakModeCharacterWrap;
                productValueLabel.numberOfLines = 2;
                productValueLabel.textAlignment = UITextAlignmentCenter;
                productValueLabel.text = [NSString stringWithFormat:@"%@",[[products objectAtIndex:m] objectAtIndex:5]];
                [order_cellView addSubview:productValueLabel];
                valueFrame.origin.y += 45;
            }
            [cell.contentView addSubview:order_cellView];
            break;
        case 1:
            email_cellView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, cell.frame.size.width,cell.frame.size.height) ];
            email_cellView.backgroundColor = [UIColor clearColor];
            email_cellView.tag =1001;


            emailAdressLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 0, cell.frame.size.width, cell.frame.size.height)];
            emailAdressLabel.tag = 100;
            emailAdressLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
            emailAdressLabel.backgroundColor = [UIColor clearColor];
            emailAdressLabel.lineBreakMode = UILineBreakModeCharacterWrap;
            emailAdressLabel.numberOfLines = 2;
            emailAdressLabel.textAlignment = UITextAlignmentLeft;
            emailAdressLabel.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:6]];
            [email_cellView addSubview:emailAdressLabel];
            [cell.contentView addSubview:email_cellView];

            break;
        case 2:

            phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, cell.frame.size.width, cell.frame.size.height)];
            phoneNumberLabel.tag = 101;
            phoneNumberLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
            phoneNumberLabel.backgroundColor = [UIColor clearColor];
            phoneNumberLabel.lineBreakMode = UILineBreakModeCharacterWrap;
            phoneNumberLabel.numberOfLines = 1;
            phoneNumberLabel.textAlignment = UITextAlignmentLeft;
            phoneNumberLabel.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:7]];
            [cell.contentView addSubview:phoneNumberLabel];

            break;
        case 3:
            billingDetails = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 160, cell.frame.size.height)];
            billingDetails.tag = 102;
            billingDetails.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
            billingDetails.backgroundColor = [UIColor clearColor];
            billingDetails.lineBreakMode = UILineBreakModeCharacterWrap;
            billingDetails.numberOfLines = 4;
            billingDetails.textAlignment = UITextAlignmentLeft;
            billingDetails.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:4]];
            [cell.contentView addSubview:billingDetails];
            break;
        case 4:
            shippingDetails = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 160, cell.frame.size.height)];
            shippingDetails.tag = 103;
            shippingDetails.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
            shippingDetails.backgroundColor = [UIColor clearColor];
            shippingDetails.lineBreakMode = UILineBreakModeCharacterWrap;
            shippingDetails.numberOfLines = 4;
            shippingDetails.textAlignment = UITextAlignmentLeft;
            shippingDetails.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:5]];
            [cell.contentView addSubview:shippingDetails];

            break;
        case 5:
            orderStatus = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.frame.size.width, cell.frame.size.height)];
            orderStatus.tag = 104;
            orderStatus.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
            orderStatus.backgroundColor = [UIColor clearColor];
            orderStatus.lineBreakMode = UILineBreakModeCharacterWrap;
            orderStatus.numberOfLines = 1;
            orderStatus.textAlignment = UITextAlignmentLeft;
            orderStatus.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:19]];
            [cell.contentView addSubview:orderStatus];

            break;
        case 6:
            deleteOrder = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.frame.size.width, cell.frame.size.height)];
            deleteOrder.tag = 110;
            deleteOrder.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
            deleteOrder.backgroundColor = [UIColor clearColor];
            deleteOrder.lineBreakMode = UILineBreakModeCharacterWrap;
            deleteOrder.numberOfLines = 1;
            deleteOrder.textAlignment = UITextAlignmentLeft;
            deleteOrder.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:19]];
            [cell.contentView addSubview:deleteOrder];

            break;

        default:
            break;
    }       
}

switch (indexPath.section) {
    case 0:
        order_cellView = (UIView *)[cell.contentView viewWithTag:1000];
        for(int m=0;m<numberOfProduct;m++){
            productLabel = (UILabel *)[order_cellView viewWithTag:m];
            productValueLabel = (UILabel *)[order_cellView viewWithTag:m+2];
        }
        if(indexPath.section == 0){
            for(int m=0;m<numberOfProduct;m++){
                productLabel.text =  [NSString stringWithFormat:@"%@ x %@ x %@",[[products objectAtIndex:m] objectAtIndex:1],[[products objectAtIndex:m] objectAtIndex:2],[[products objectAtIndex:m] objectAtIndex:3]];
                productValueLabel.text = [NSString stringWithFormat:@"%@",[[products objectAtIndex:m] objectAtIndex:5]];
            }
        }
        break;
    case 1:
        email_cellView = (UIView *)[cell.contentView viewWithTag:1001];
        emailAdressLabel = (UILabel *)[email_cellView viewWithTag:100];
        emailAdressLabel.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:6]];      
        break;
    case 2:
        phoneNumberLabel = (UILabel *)[cell.contentView viewWithTag:101];
        phoneNumberLabel.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:7]];
        break;
    case 3:
        billingDetails = (UILabel *)[cell.contentView viewWithTag:102];
        billingDetails.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:4]];
        break;
    case 4:
        shippingDetails = (UILabel *)[cell.contentView viewWithTag:103];
        shippingDetails.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:5]];
        break;
    case 5:
        orderStatus = (UILabel *)[cell.contentView viewWithTag:104];
        orderStatus.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:19]];
        break;
    case 6:
        deleteOrder = (UILabel *)[cell.contentView viewWithTag:110];
        deleteOrder.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:19]];
        break;
    default:
        break;
}

return cell;

}

Upvotes: 0

Views: 2203

Answers (3)

user3575114
user3575114

Reputation: 993

replace CellIdentifier with nil-

[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

It is because you are reusing the cell identifier so data is overlapping

Upvotes: 0

anand madhav
anand madhav

Reputation: 353

just needs to give different identifier for each cell you are declaring so if you will do that they will identify the cell and data will not merge on each other

and one more sol is to give each cell subview tag and view the cell with subviews tag..

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52565

I think your main problem is that you have seven different "types" of cell but only one "reuse identifier." This means that an order cell could be later reused as a delete order cell. However, when reused, the cell won't necessarily have all the elements it needs to display fully.

For example, email cell has a tag of 1001 and delete cell has a tag of 110, but not vice-versa.

Upvotes: 2

Related Questions