2016-08-17 37 views
0

我想通过单击单元格上的按钮来展开和折叠我的uitableview中的单元格数组。我不想展开或折叠单击的单元格,但后续的子单元格。此外,我想展开和折叠只在数组中的项目。展开/折叠UITableView中的单元格阵列

目前,我有以下代码,但它没有做任何关于更新单元格的事情。

我在做什么错?

- (void)reloadTableView:(UITableView *)tableView 
{ 
    NSArray *itemsArray = self.childItems; 

    NSMutableArray *insertPath = [NSMutableArray array]; 
    NSMutableArray *deletePath = [NSMutableArray array]; 

    [itemsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     if (![itemsArray containsObject:obj]) { 
      [insertPath addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 
     } 
    }]; 

    [itemsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     if ([itemsArray containsObject:obj]) { 
      [deletePath addObject:[NSIndexPath indexPathForRow:idx inSection:0]]; 
     } 
    }]; 

    if ([insertPath count] || [deletePath count]) { 
     [tableView beginUpdates]; 

     if ([deletePath count]) { 
      [tableView reloadRowsAtIndexPaths:deletePath withRowAnimation:UITableViewRowAnimationFade]; 
     } 

     if ([insertPath count]) { 
      [tableView reloadRowsAtIndexPaths:insertPath withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     [tableView endUpdates]; 
    } 
    else { 
     [tableView reloadData]; 
    } 
} 

回答

1

您只重新加载要删除或插入的行。相反,删除要删除的行并插入要插入的行。

[tableView beginUpdates]; 

if ([deletePath count]) { 
    [tableView deleteRowsAtIndexPaths:deletePath withRowAnimation:UITableViewRowAnimationFade]; 
} 

if ([insertPath count]) { 
    [tableView insertRowsAtIndexPaths:insertPath withRowAnimation:UITableViewRowAnimationFade]; 
} 
[tableView endUpdates]; 
+0

感谢马迪...但我总是得到无效的行数在部分。 – lifemoveson

+0

在更新表格视图之前,您需要更新表格视图的数据源。 – rmaddy

+0

你的意思是添加removeObjectAtIndex? – lifemoveson