views:

53

answers:

1

Hi,

I have a singleton class which is used to display data across various views.

There is one TableView which is used to delete/insert rows. I have a button that changes between Edit/Done to allow Editing. 'streams is a variable within the Singleton class'

- (void)setEditing:(BOOL)flag animated:(BOOL)animated{   

       int count = [streams count];


    UITableView *tableView = (UITableView *)self.view;
    NSArray *topIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]];

 if (self.editing == YES)
 {NSLog(@"EDITING");
 [tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];}

 else{NSLog(@"NOT EDITING");
 [tableView deleteRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];}


}

And use editingStyleForRowAtIndexPath to allow choose which editing style is used for each row.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

      int count = [streams count];
         int row = indexPath.row ;

 if (row == count)

       return    UITableViewCellEditingStyleInsert;

   else

       return UITableViewCellEditingStyleDelete;}

I use cellForRowAtIndexPath for to create an additional row with the text " Add phone Number" when in edit mode.

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

static NSString *DeleteMeCellIdentifier = @"AudioCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                         DeleteMeCellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:DeleteMeCellIdentifier] autorelease];
}
int x = indexPath.row;
if (self.editing == YES)
{
    if (indexPath.row == [streams count])

        cell.textLabel.text = @"Add Phone Number";

    else
        cell.textLabel.text = [self.streams objectAtIndex:indexPath.row];
}
else
{
    cell.textLabel.text = [self.streams objectAtIndex:indexPath.row];
}

return cell;}

Deleting Rows works fine. On selecting to insert a row another view is pushed onto the stack. From this view the user is given a number of text options to label the new row we've just created. The following code is used to update the singleton class once a selection has been made.

- (void)viewWillDisappear:(BOOL)animated {

Disc *disc = [Disc sharedDisc];
[disc.streams insertObject:@"0208" atIndex:0];
[super viewDidAppear:animated];}

Once the text for the row has been selected to user has to select the back button to get back to the previous TableView. Here's when the problem arises. In the main TableView, instead of one option labelled "Add phone Number" there are two.

I know that the singelton class which the TableView is using has been updated, it's the TableView that doesn't update in the correct manner. If I then switch between Edit/Done the tableView displays the information correctly.

I have tried to update the singleton class in the ViewDidLoad & ViewWillAppear methods but the result is the same, the first time that the view is reloaded it doesn't display the new row properly.

I have thought about overriding the "Back" BarButton to try and get the TableView to display correctly.

Many Thanks

A: 

I didn't understand quite why duplicate rows had to do with updating the tableview. But it sounds a bit like you're creating two tables, somehow? Or even two viewcontrollers on top of each other, but I'll assume your navigation code is set up properly (using tabbars can cause this with incorrect viewcontroller initialization code).

You'd definitely want anything that changes the tables contents in ViewWillAppear.

Do you get the duplicate cells even when updating in ViewDidLoad (normally only called the first time the view appears). To execute when navigating back,

If you are looking to refresh the table data, use [tableview reloadData] in ViewWillAppear.

If all else fails, to trace this bug, I recommend to Add Expression on the tableView object and watch it change (in Debug mode).

Henrik Erlandsson
Thanks Henrik. The duplicates do appear regardless of using ViewDidLoad or ViewWillAppear. I should have mentioned that yes, there are two ViewControllers used. If I edit the code so that adding a row doesn't push a new view but instead updates the Singeton class and then calls [tableView Reload data] it works perfectly. The problem arises when updating the singelton from a second tableView and navigating back to the first tableView.