sdev
sdev

Reputation: 113

Row Order in UITableViewCell is wrong

This is a simple class that extended UITableViewController, there's only one section total 5 rows. but when launch app, only show 2 record, when pressed the button, it will show more records with text field.

but, press simple button and press detail button , i found the order in table is wrong.

I don't know what happen, so can help me to solve this problem? Many thanks enter image description here

- (void)viewDidLoad
{
    [super viewDidLoad];

    tempSimpleAry = [@[@"simple1",@"simple2"] mutableCopy];
    tempDetailAry = [@[@"detail A",@"detail B",@"detail C"] mutableCopy];
    dataAry = [[NSMutableArray alloc]init];
    [dataAry addObjectsFromArray:tempSimpleAry];

    isExpanded = NO;
}

-(void)dealloc{
    [super dealloc];
    tempSimpleAry = nil;
    tempDetailAry = nil;
    dataAry = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

- (BOOL)textFieldShouldReturn:(UITextField *)aTextField {
    [aTextField resignFirstResponder];
    return YES;    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataAry.count+1;
}


static NSString *BasicCellIdentifier = @"BasicCell";
static NSString *MyCellIdentifier = @"MyCell";
static NSString *LastCellIdentifier = @"LastCell";

UITableViewCell *Lastcell = nil;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    if(indexPath.row<tempSimpleAry.count){

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BasicCellIdentifier forIndexPath:indexPath];
        cell.textLabel.text =  [[NSString alloc]initWithFormat:@"%@ %d", [dataAry objectAtIndex:row], row];

        return cell;
    }else if(indexPath.row < dataAry.count){
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier forIndexPath:indexPath];

        cell.myTitleLabel.text =  [[NSString alloc]initWithFormat:@"%@ %d", [dataAry objectAtIndex:row], row];
        cell.myTextField.placeholder =  [[NSString alloc]initWithFormat:@"%@ %d", [dataAry objectAtIndex:row], row];
        NSLog(@"MyTableViewCell - myTitlelabel = %@, mytextfield tag = %d, text = %@", cell.myTitleLabel.text, cell.myTextField.tag,cell.myTextField.text);
        cell.myTextField.tag =100+indexPath.row;

        return cell;
    }else{
        Lastcell = [tableView dequeueReusableCellWithIdentifier:@"LastCell" forIndexPath:indexPath];

            Lastcell.textLabel.text = @"Detail";

        return Lastcell;
    }


}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [currentEditingTextField resignFirstResponder];

    NSMutableArray *tmpAry = [[NSMutableArray alloc]init];

    if(indexPath.row == dataAry.count){
        if(isExpanded){
            int idx = dataAry.count-1;            
            for(int i=0; i<tempDetailAry.count;i++){
                NSIndexPath *idxPaths = [NSIndexPath indexPathForRow:idx-i inSection:0] ;
                [tmpAry addObject:idxPaths];
                [dataAry removeLastObject];
            }

            [tableView deleteRowsAtIndexPaths:tmpAry withRowAnimation:UITableViewRowAnimationFade];

        }else{            
            int idx = tempSimpleAry.count;

            for(int i=0; i<tempDetailAry.count;i++){
                NSIndexPath *idxPaths = [NSIndexPath indexPathForRow:idx+i inSection:0] ;
                [tmpAry addObject:idxPaths];
                [dataAry insertObject:[tempDetailAry objectAtIndex:i] atIndex:idx+i];

            }

            [tableView insertRowsAtIndexPaths:tmpAry withRowAnimation:UITableViewRowAnimationFade];
        }

        [self.tableView beginUpdates];
        [self.tableView endUpdates];

        isExpanded = !isExpanded;
        if(!isExpanded)
            Lastcell.textLabel.text = @"Detail";
        else
            Lastcell.textLabel.text = @"Simple";
    }


}

Upvotes: 0

Views: 530

Answers (2)

Jason Lee
Jason Lee

Reputation: 3250

It is because the cells are cached with cellIdentifier.

When you press Simple, you delete cells by the sequence, 'detail C 4', 'detail B 3', 'detail A 2'. So the 'detail C 4' cell is pushed into the cache queue first, then 'detail B 3' cell, finally 'detail C 2' cell.

Then you press Detail, the tableView query the cache queue one by one. Because the 'detail C 4' cell is at the front, it is popped first. So u use the cell with 'aa' in the textField, and then you set the cell.myTitleLabel.text and cell.myTextField.placeholder, but you don't set the cell.myTextField.text, which is 'aa'. Finally, the cell became 'detail A 2' with text 'aa' in the textField.

The same with the other two cells.

Is the above clear for you ?

Upvotes: 1

Asif Mujteba
Asif Mujteba

Reputation: 4656

It seems that u are only changing the textfield's place holder not its text, You should also save the textfield's text into an array and set it in cellForRowAtIndexPath!

Upvotes: 0

Related Questions