[英]iOS - UITableView Delete All Rows Within A Section
我有一個帶有可擴展部分的UITableView
。 當用戶轉到另一個視圖時,我需要折疊所有展開的部分,並將其放入viewWillDisappear
方法中。
我只找到了關於如何一次刪除表視圖中所有行的解決方案,但是有沒有辦法刪除特定部分中的所有行呢?
編輯:
我已經找到了解決方案,但是我不確定這是最佳選擇還是將來會導致效率低下。 每當單元被擴展時,它就會被添加到NSMutableIndexSet
。 因此,在我的viewWillDisappear
方法中,我遍歷擴展的部分,如下所示:
-(void)viewWillDisappear:(BOOL)animated
{
if (expandedSections.count != 0) {
NSLog(@"COLLAPSING CALLED");
[self.tableView beginUpdates];
NSUInteger section = [expandedSections firstIndex];
do
{
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
rows = [self tableView:self.tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
for (int i=1; i<rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
[self.tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
NSIndexPath *expandableCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:expandableCellIndexPath];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[self.colorHolder objectAtIndex:section] type:DTCustomColoredAccessoryTypeRight];
section = [expandedSections indexGreaterThanIndex:section];
} while (section != NSNotFound);
[self.tableView endUpdates];
}
}
請告訴我這是否是一個好的解決方案,或者如果我懷疑正確,那么當每個擴展部分中有更多行時,這是否會導致將來視圖之間的轉換變慢。 任何幫助或建議,將不勝感激。 謝謝。
如果要動畫化更改,則需要先更新數據源(以返回0表示該部分中的行數),然后刪除該部分,並在[tv beginUpdates]
[tv endUpdates]
[tv beginUpdates]
之間的一個事務中的同一索引路徑處添加該部分。 [tv endUpdates]
否則,只需更新數據源並在返回VC的途中重新加載表即可(如果您不需要任何動畫)
我沒有詳細閱讀,但是肯定for循環應該從零開始。
for (int i=0; i<rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
否則,您將只刪除該部分中除第一個單元格外的所有單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.