Почему данные UITableview перекрываются? - PullRequest
0 голосов
/ 10 ноября 2011

Почему данные таблиц перекрываются? я приложил свой код.

Если я снова объясню свой код, тогда

  1. Данные xml находятся в массиве records, который отображается в виде таблицы.
  2. В первый раз, когда программа выполняется, тогда данные находятся в нужном месте, но когда я начинаю прокручивать вертикально, данные начинают отображаться в различных ячейках. Предположим, первая ячейка отображается в последней ячейке, данные второй ячейки отображаются в первой ячейке ..... так на.

ПРИМЕЧАНИЕ, что: для проблемы с перекрытием я также использовал switch и viewwithtag, но все еще сейчас tableview данные начинают отображаться неправильно cell, когда я начинаю прокрутку на 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;

}

Ответы [ 3 ]

2 голосов
/ 10 ноября 2011

Я думаю, что ваша главная проблема в том, что у вас есть семь различных «типов» ячеек, но только один «идентификатор повторного использования». Это означает, что ячейка заказа может быть позже использована в качестве ячейки заказа на удаление. Однако при повторном использовании ячейка не обязательно будет иметь все элементы, необходимые для полного отображения.

Например, ячейка электронной почты имеет тег 1001, а ячейка удаления имеет тег 110, но не наоборот.

1 голос
/ 09 августа 2013

просто нужно присвоить разные идентификаторы для каждой ячейки, которую вы объявляете, поэтому, если вы это сделаете, они будут идентифицировать ячейку, и данные не будут сливаться друг с другом

и еще одно решение - присвоить каждому тегу subview видения и просмотреть ячейку с тегом subviews.

0 голосов
/ 28 июля 2014

заменить CellIdentifier на nil-

[tableView dequeueReusableCellWithIdentifier: CellIdentifier];

Это потому, что вы повторно используете идентификатор ячейки, поэтому данные перекрываются

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...