UITableView discloure indicator going nuts

I have a UITableView (on a UIViewController) which is pushed via a navigationController. Along with pushing, I select with which array i want to populate the table. The code for pushing is like this:

if(self.newView == nil)
{
    NewView *viewTwo = [[NewView alloc] initWithNibName:@"Bundle" bundle:[NSBundle mainBundle]];
    self.newView = viewTwo;
    [viewTwo release];
}

[self.navigationController pushViewController:self.newView animated:YES];
newView.tableArray=newView.arrayWithOptionOne;
[newView.tableView reloadData];

All works well and the table gets reloaded every time. However in the last row of section 0, there is a switch which loads section 1. The last row of section 1 is tappable (didSelect…) and it loads a modalView. On this last rod I added a disclosure indicator and also the blue background when tapping. The table has sliders, labels, etc. So the customization is quite long:

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

UITableViewCell *cell = nil;


static NSString *kDisplayCell_ID = @"DisplayCellID";

cell = [tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID];

if (cell == nil)
    {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:kDisplayCell_ID] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
else
    {

    UIView *viewToRemove = nil;
    viewToRemove = [cell.contentView viewWithTag:1];
    if (viewToRemove)
        [viewToRemove removeFromSuperview];

    UIView *viewToRemove2 = nil;
    viewToRemove2 = [cell.contentView viewWithTag:2];
    if (viewToRemove2)
        [viewToRemove2 removeFromSuperview];


 }


    if (indexPath.section==0) {
        UIControl *cellValueS = [[[arrayTable objectAtIndex:indexPath.section] objectForKey:kViewKey] objectAtIndex:indexPath.row];
        [cell.contentView addSubview:cellValueS];
    }

    if (indexPath.section==0 && indexPath.row==3) {
        UIControl *cellValueL = [[[arrayTable objectAtIndex:indexPath.section] objectForKey:kViewLabel] objectAtIndex:0] ;
        [cell.contentView addSubview:cellValueL];
    }

    if (indexPath.section==1 && indexPath.row==0){
        UIControl *cellValueS = [[[arrayTable objectAtIndex:indexPath.section] objectForKey:kViewKey] objectAtIndex:indexPath.row] ;
        [cell.contentView addSubview:cellValueS];
    } 

    if (indexPath.section==1 && indexPath.row==1) {
        UIControl *cellValueS = [[[arrayTable objectAtIndex:indexPath.section] objectForKey:kViewKey] objectAtIndex:indexPath.row] ;
        [cell.contentView addSubview:cellValueS];

        UIControl *cellValueL = [[[arrayTable objectAtIndex:indexPath.section] objectForKey:kViewLabel] objectAtIndex:0] ;
        [cell.contentView addSubview:cellValueL];
    }

    if (indexPath.section==1 && indexPath.row==2) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

So far also works ok.

The problem is that when I go back to the previous view and select another option to populate the table, when it's pushed again, I see the disclosure indicator and blue selection background on other rows on the same section. I've observed that it depends on where the table is scrolled.

I've tried to understand why does it happen, but i can't. I've somehow solved the problem by setting newView to nil and releasing it and then allocating it again before it gets pushed again.

Am I doing something wrong here? or why is the disclosure indicator and tapping background appearing where they are not supposed to be?

Thanks in advance!

action of the switch

-(void)extraOptionsSwitchAction:(id)sender{



 switch(extraOptionsSwitch.isOn) {

 case 1:

 // [self.tableView beginUpdates];
 [self.tableView insertSections: [NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationAutomatic];
 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

 //[self.tableView reloadData];
 //  [self.tableView endUpdates];

 break;

 case !1:

 //   [self.tableView beginUpdates];
 [self.tableView deleteSections: [NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationAutomatic];
 // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

 [self.tableView reloadData];
 // [self.tableView endUpdates];

 break;

 }
 }

Answers


It has to to with reusing cells. You probably don't differentiate between the different cell types in your cell creation method - so one cell that went offscreen can easily be reused for another (different type) cell. Further, you seem to add subviews over and over again - only do that when you instantiate the cell (with alloc/int), and not when configuring.

Also, for different cell types, use different identifiers (you didn't show this code).

The programming guides have good example on table views and their cells and reuse pattern. It's worth reading a couple of times - it's easy to get wrong and is a main topic for performance tuning.

Edit

Now that you added more code, another problem seems to be here:

if (indexPath.section==1 && indexPath.row==2) {
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}

You are missing an else part. The selectionStyle and accessoryType are set to what they were set to before - you miss to configure the cells correctly for all other cells than that special one.

Each cell type should really get its own identifier though. If the adding/removing of subviews work as expected is hard to tell from that code.

One thought: As you aren't really reusing a lot of the cells here anyhow you could even disable the reuse by changing

static NSString *kDisplayCell_ID = @"DisplayCellID";
cell = [tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID];

to

static NSString *kDisplayCell_ID = nil;

This would just always produce a new cell. I wouldn't recommend this in the general case, though.


Need Your Help

How to monitor incoming and outgoing IP traffic on Android

android linux networking proc

I have to write a network monitoring app for android and one of the requisites is to monitor all the incoming and outgoing IP traffic, I found out the /proc/stat/netstat file which I'm pretty sure

How to save qlineedit fields into an XML file using Python for loops?

python xml pyqt minidom

I'm trying to read all my QLineEdit fields and the checkBox states and save them to an XML file using Minidom. Below is what I have so far. What is the easiest and shortest way I can write this u...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.