project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
branchios-revival
changeset 11253 62d1a66b353d
parent 11250 7f3f96e9fdf1
child 11306 7a9464f9e992
--- a/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m	Thu Oct 29 03:48:42 2015 +0100
+++ b/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m	Thu Oct 29 23:59:51 2015 +0100
@@ -22,7 +22,7 @@
 
 
 @implementation SavedGamesViewController
-@synthesize tableView, listOfSavegames, numberOfItems;
+@synthesize tableView, listOfSavegames;
 
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
     return rotationManager(interfaceOrientation);
@@ -33,7 +33,6 @@
     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
     self.listOfSavegames = array;
     [array release];
-    self.numberOfItems = [self.listOfSavegames count];
 
     [self.tableView reloadData];
 }
@@ -96,13 +95,12 @@
         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
 
         // update the table and the cached list
-        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:self.numberOfItems];
-        for (int i = 0; i < self.numberOfItems; i++)
+        NSMutableArray *array = [[NSMutableArray alloc] init];
+        for (int i = 0; i < [self.listOfSavegames count]; i++)
             [array addObject:[NSIndexPath indexPathForRow:i inSection:0]];
-        self.numberOfItems = 0;
+        [self.listOfSavegames removeAllObjects];
+        
         [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
-        [self.listOfSavegames removeAllObjects];
-
         [array release];
     }
 }
@@ -114,21 +112,19 @@
 }
 
 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-    return self.numberOfItems;
+    return [self.listOfSavegames count];
 }
 
 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"Cell";
 
-    if (self.listOfSavegames == nil)
-        [self updateTable];
     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (editableCell == nil) {
         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
-        editableCell.delegate = self;
+        editableCell.delegate = nil;
+        editableCell.textField.userInteractionEnabled = NO;
     }
     editableCell.tag = [indexPath row];
-    editableCell.respectEditing = YES;
     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 
@@ -158,15 +154,30 @@
 }
 
 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
-    [((EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath]).textField resignFirstResponder];
-    
-    self.numberOfItems--;
-    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
+    [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
+    [self fixTagsForStartTag:[indexPath row]];
 
     NSString *saveName = [self.listOfSavegames objectAtIndex:[indexPath row]];
     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
     [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
     [self.listOfSavegames removeObject:saveName];
+    
+    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
+}
+
+#pragma mark - Fix cells' tags
+
+- (void)fixTagsForStartTag:(NSInteger)tag
+{
+    for (UITableViewCell *cell in self.tableView.visibleCells)
+    {
+        NSInteger oldTag = cell.tag;
+        
+        if (oldTag > tag)
+        {
+            cell.tag--;
+        }
+    }
 }
 
 #pragma mark -
@@ -189,7 +200,6 @@
     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
     [newFilePath release];
 
-    self.numberOfItems++;
     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
 
     [GameInterfaceBridge registerCallingController:self];