2016-08-17 38 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