Ali
Ali

Reputation: 643

TableView content changes after device rotating

I have an application with a table view that contains 3 sections. Every thing is okay with displaying data, but when i change the device orientation (from portrait to Landscape and vis versa ) an additional number of rows is added to my table View

Indeed, when debugging my Application, i figured out that cellForRowAtIndexPath: is executed every time i rotate the device.

Can some Good person tell me how can i fix that problem ?

Thanks in advance.

Upvotes: 0

Views: 187

Answers (2)

rmaddy
rmaddy

Reputation: 318934

As suspected, your issue is that you are adding items to your data source data in the tableView:numberOfRowsInSection: method. Every time the table asks how many rows are in a section, you add more data to the data structures. Remember, the data source methods will be called many different times.

You need to create your data structures once, outside of any of the data source methods. Your data source methods should be read-only with regard to your data structures. They should simple return a count. No other processing should be done in the data source methods.

Upvotes: 1

Ali
Ali

Reputation: 643

Here's the code !!!

`- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
    for (int i=0; i < self.TiersArray.count; i++) {
         if (([[[self.TiersArray objectAtIndex:i]t_client] isEqualToNumber:one])||        ([[[self.TiersArray objectAtIndex:i]t_client] isEqualToNumber:three])) 
   {
            [ClientsArray addObject:[self.TiersArray objectAtIndex:i]];
        }

    }
return self.ClientsArray.count;
}
else if(section==1){
    for (int i=0; i < self.TiersArray.count; i++) {
        if (([[[self.TiersArray objectAtIndex:i]t_client]isEqualToNumber:two])||([[[self.TiersArray objectAtIndex:i]t_client]isEqualToNumber:three])) {
            [ProspectsArray addObject:[self.TiersArray objectAtIndex:i]];
        }

    }
    return self.ProspectsArray.count;
}
else{
    for (int i=0; i < self.TiersArray.count; i++) {
        if ([[[self.TiersArray objectAtIndex:i]t_fournisseur] isEqualToNumber:one ]) {
            [FournisseurArray addObject:[self.TiersArray objectAtIndex:i]];
        }

    }
    return self.FournisseurArray.count;
}
}

`

Upvotes: 0

Related Questions