views:

42

answers:

2

I have a tableview with four sections first section has 7 rows in that first six rows has a textfield and the last row has two textfields

Section 1

t1
t1
t1
t1
t1
t1
t1 t2

section 2

t1 t2
t1 t2 // second row textfields are placed in fourth rows
t1 t2
t1 t2 // fourth row textfields are placed in someother rows
t1 t2
t1 t2
t1 t2

section 3

t1 t2
t1 t2
t1 t2
t1 t2
t1 t2
t1 t2
t1 t2

section 4

t1
t1
t1
t1
t1
t1
t1
t1

Second & third section contains seven rows having two textfields each

Fourth section has eight rows containing one textfield in each row i have assigned unique tags to all the textfields

In textFieldDidEndEditing i am saving the contents to an array in appropriate index.

If i am scrolling the tableview after entering datas in the textfield.Some textfield positions are changed (i.e) textfield in the second row are placed in the third row similarly some rows are swapped.

I have created 5 cell identifiers one for the first six rows in section one ,second for the last row in the first section and remaining three cell identifiers for the other sections.

When i scroll some times cell.contentview viewWithtag returns nil value for tag

I think this is the reason for wrong textfield in some rows.

How to rectify this problem ?

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

NSString *identifier;<bn>
UITableViewCellStyle style = UITableViewCellStyleDefault;<br>
BOOL selectable = NO;<br>

switch (indexPath.section)
 {
    case 0:
    {
        if(indexPath.row < 6)
            identifier = @"firstsectionSixRows";
        else 
            identifier = @"firstsectionLastRow";
        break;
    }
    case 1:
        identifier = @"secondsection";
        break;
    case 2:
        identifier = @"thirdsection";
        break;
    case 3:
        identifier = @"fourthsection";
        break;

}

return [self createCellForIdentifier:identifier
                           tableView:tableView
                           indexPath:indexPath
                               style:style
                          selectable:selectable];

}

In createCellForIdentifier method

else if([identifier isEqualToString:@"secondsection"])
{
    int TextTag = 8 + indexPath.row * 2;



    UILabel *lblName = (UILabel*)[cell.contentView viewWithTag:10000];

    lblName.text = [NSString stringWithFormat:@"G%d",indexPath.row + 1];

    //NSLog(@"row = %d texttag = %d",indexPath.row,TextTag);    

    UITextField *txtFirst = (UITextField*) [cell.contentView viewWithTag:TextTag];

    if([textFieldArray count] > 0)
    {
        if(![[textFieldArray objectAtIndex:TextTag] isEqualToString:@""])   
        {
            if(txtFirst)
                txtFirst.text = [NSString stringWithFormat:@"%@",[textFieldArray objectAtIndex:TextTag]];   
            else
                NSLog(@"Textfield is nil \n tag = %d \n cell section = %d row = %d",TextTag,indexPath.section,indexPath.row);

        }
    }

    UITextField *txtSecond = (UITextField*) [cell.contentView viewWithTag:TextTag + 1];


    if([textFieldArray count] > 0)
    {
        if(![[textFieldArray objectAtIndex:TextTag + 1] isEqualToString:@""])   
        {
            if(txtSecond)
                txtSecond.text = [NSString stringWithFormat:@"%@",[textFieldArray objectAtIndex:TextTag + 1]];  
            else
                NSLog(@"Textfield is nil \n tag = %d \n cell section = %d row = %d",TextTag + 1,indexPath.section,indexPath.row);
        }
    }
}
A: 

I'd recommend you write a method to get the index of the text based on the indexPath... you likely have a mistake between the setting of the object and the getting. Also... I wouldn't use an array for this with crazy lookups like that. You should probably use a NSDictionary with a key = "{section}_{row}" or something like that.

For a quick debug, I'd recommend putting a breakpoint in and seeing what is in your array, you can do that with po textFieldArray in the console.

Michael Kernahan
i cannot able to get the exact textfield for that tag in that row.When i print the cell subviews the textfield with different tags are inside that cell so [cell.content viewfortag:TextTag] is returning nil
Riswan