开发者

rows are interchanging in uitableview in iphone?

开发者 https://www.devze.com 2023-04-07 23:49 出处:网络
In my application i need to add contacts of users, for that i am using UITableview with sections. Each section may contains 2 or 3 rows.

In my application i need to add contacts of users, for that i am using UITableview with sections. Each section may contains 2 or 3 rows. w开发者_如何学编程hile the editing was completed and scrolling the table view to top, the rows are interchanging in between sections. Actually In design part i have placed FirstName and LastName in first section and email in second section but they are interchanging as firstName, email as first section and LastName as second section . can any one help me to solve this. Thanks in advance.

EDIT:

Please find the code snippet below.

the method CellForRowAtIndexPath:

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;



        if(indexPath.section == 0) 
        {

            if(indexPath.row == 0) 
            {
//content of first Name

                txtFieldFirstName = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 290, 24)];
                txtFieldFirstName.tag=1;
                //txtFieldFirstName.placeholder = @"First";
                txtFieldFirstName.delegate = self;
                txtFieldFirstName.font = [UIFont fontWithName:@"Helvetica" size:16.0];
                [cell.contentView addSubview:txtFieldFirstName];
            }

            else if(indexPath.row == 1) {
            //content of last Name
            }
        }

        else if(indexPath.section == 1) 
        {

            if(indexPath.row == 0) {
//content of Phone No
            }

            else if(indexPath.row == 1) {
           //content of Mobile no
            }

this is how i am implementing my code. Please help me on this.


You are doing everything inside

if (cell == nil)

This code is only executed when you first create a cell. If the table view returns a cell that has already been created, cell != nil so the code is not executed.

You should only perform base configuration inside cell == nil, everything else should be done afterwards. You may need to have different reuse identifiers for cells in different sections if the layout is not the same.


The issue is that your cells are being reused, and they are carrying with them the values from the previous contact. It may look like the rows are interchanging between sections, but actually they are interchanging between contacts.

This sets the cell content outside of the if (cell == nil) so would address the issue in many cases:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

if(indexPath.section == 0) 
{
    if (indexPath.row == 0) 
    {
        //content of first Name
        txtFieldFirstName = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 290, 24)];
        txtFieldFirstName.tag=1;
        //txtFieldFirstName.placeholder = @"First";
        txtFieldFirstName.delegate = self;
        txtFieldFirstName.font = [UIFont fontWithName:@"Helvetica" size:16.0];
        [cell.contentView addSubview:txtFieldFirstName];
    }

    else if (indexPath.row == 1) {
        //content of last Name
    }
}

else if(indexPath.section == 1) 
{

    if(indexPath.row == 0) {
        //content of Phone No
    }

    else if(indexPath.row == 1) {
        //content of Mobile no
    }
}

However, if some cell properties are set on some occasions, and only others are used on other occasions, you may need to explicitly set the unused cell properties to nil in each case so that they do not carry over values from the previous contact they displayed, then they are reused in the dequeue process.


Actually this is happening due to reuse identifier of your table cell

Try below Code

NSString *CellIdentifier=[NSString stringWithFormat:@"cell%d",indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];            
}
0

精彩评论

暂无评论...
验证码 换一张
取 消