- Refactoring of SavedViewController: ios-revival
authorantonc27 <antonc27@mail.ru>
Thu, 29 Oct 2015 23:59:51 +0100
branchios-revival
changeset 11253 62d1a66b353d
parent 11252 3dc674af7662
child 11254 407045ffe343
- Refactoring of SavedViewController: - - Workaround for cells' wrong tags in SavedViewController when removing saves - - Disabled editing of save names
project_files/HedgewarsMobile/Classes/SavedGamesViewController.h
project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
--- a/project_files/HedgewarsMobile/Classes/SavedGamesViewController.h	Thu Oct 29 03:48:42 2015 +0100
+++ b/project_files/HedgewarsMobile/Classes/SavedGamesViewController.h	Thu Oct 29 23:59:51 2015 +0100
@@ -24,12 +24,10 @@
                                                         EditableCellViewDelegate, UIActionSheetDelegate>  {
     UITableView *tableView;
     NSMutableArray *listOfSavegames;
-    NSInteger numberOfItems;
 }
 
 @property (nonatomic,retain) IBOutlet UITableView *tableView;
 @property (nonatomic,retain) NSMutableArray *listOfSavegames;
-@property (assign) NSInteger numberOfItems;
 
 -(IBAction) buttonPressed:(id) sender;
 
--- 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];