2015-02-08 49 views
1

在此先感谢。我正在处理一个单独的UITableView,它被分成不同的部分。用户只能从特定部分添加或删除单元格。在UITableView的显示的信息被存储在一个阵列,其中每个对象表示一个部分:Xcode iOS UITableView插入新单元格动画

例如:tableItemsArray =((arrayForSection1),(arrayForSection2),(arrayForSection3))

我有动画设置处理删除如下:

[tableView deleteRowsAtIndexPaths:@[indexPath] 
         withRowAnimation:UITableViewRowAnimationRight]; 

然后,我以编程方式从数组中删除项目以将其从数据模型中删除。不过,我很难在如何以动画形式插入单元格。

目前我当前运行的伪算法:

  1. 检查项目是不是数组
  2. 到阵列添加到项目的特定部分
  3. 排序阵列的这一部分按字母顺序中的任何地方
  4. 重新加载数据

更新

当新项目添加到数组时,它将存储在一个字符串中以保持新项目的记录。一旦数组已被使用,我遍历单元格数组以找到它的indexPath,然后删除字符串(以防止递归插入动画)并执行动画,但是代码不会将其插入到表格中。我想知道是否可能动画代码(如下所示)不工作,因为我正在使用[tableView reloadData]?

// For Each Section in the table: 
     for (NSMutableArray *object in sectionsInTable) { 

      // For Each Cell in a section (Cells are stored in an array in index 1) 
      for (NSString *label in [object objectAtIndex:1]) { 
       if ([label isEqualToString:_insertedObject]) { 
        // New item 
        _insertedObject = Nil; 
        // animate addition 
        [self.labelListTableView beginUpdates]; 
        [self.labelListTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[[object objectAtIndex:1] indexOfObject:label] inSection:[sectionsInTable indexOfObject:object]]] withRowAnimation:UITableViewRowAnimationBottom]; 
        [self.labelListTableView endUpdates]; 
       } 
      } 
     } 

任何想法为什么动画不工作?

由于

d

+1

我有点困惑“排序算法并没有最终为我提供项目新位置的indexPath”。如果问题是你不知道如何弄清楚你想改变你的问题的索引。此外,如果是这种情况,你可能正在寻找[myArray indexOfObject:newItem];然后根据对象的部分和索引创建索引路径。希望有帮助。 – 2015-02-09 00:16:08

+0

感谢您的帮助。我在上面扩展了我的信息:-) – dgee4 2015-02-09 08:12:53

回答

0

似乎[的tableView reloadData]是防止动画。

// Annimate the new Item 
     // For Each Section in the table: 
     for (NSMutableArray *object in sectionsInTable) { 
      // For Each Cell in a section: 
      for (NSString *cell in [object objectAtIndex:1]) { 
       if ([cell isEqualToString:_insertedObject]) { 
        // New item 
        _insertedObject = Nil; 
        // animate addition 
        [tableView beginUpdates]; 
        NSInteger section = [sectionsInTable indexOfObjectIdenticalTo:object]; 
        NSInteger row = [[object objectAtIndex:1] indexOfObjectIdenticalTo:cell]; 

        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
        [tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom ]; 
        [tableView endUpdates]; 
       } 
      } 
     }