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