2013-07-16 90 views
2

我想展開/摺疊表格視圖sections.I搜索並找到一個代碼,它的工作正常。但問題是以前打開的部分不打開時,一個新的部分打開。謝謝展開和摺疊tableView部分

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{ 

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag]; 
if (indexPath.row == 0) { 
    BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue]; 
    collapsed  = !collapsed; 
    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]]; 
    //reload specific section animated 
    NSRange range = NSMakeRange(indexPath.section, 1); 
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range]; 
    [self.aTableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade]; 
} 
} 

我試過下面的代碼工作正常,但動畫並不酷,因爲所有的部分都重新加載。

NSMutableArray *isSectionTouched =[[NSMutableArray alloc]initWithCapacity:arrayForBool.count]; 
isSectionTouched=[arrayForBool mutableCopy]; 

for(int i = 1; i <[arrayForBool count] ; i ++){ 
    if(i != gestureRecognizer.view.tag){ 
     [isSectionTouched replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]]; 
    }else{ 
     if ([[isSectionTouched objectAtIndex:gestureRecognizer.view.tag]boolValue]==YES) { 
      [isSectionTouched replaceObjectAtIndex:gestureRecognizer.view.tag withObject:[NSNumber numberWithBool:NO]]; 
     }else if ([[isSectionTouched objectAtIndex:gestureRecognizer.view.tag]boolValue]==NO){ 
      [isSectionTouched replaceObjectAtIndex:gestureRecognizer.view.tag withObject:[NSNumber numberWithBool:YES]]; 
     } 
    } 
} 
arrayForBool=isSectionTouched; 
NSRange range = NSMakeRange(1,arrayForBool.count - 1); 
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range]; 
[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade]; 
+0

這個問題可以幫助你http://stackoverflow.com/questions/1938921/expand-collapse-section-in-uitableview – rdelfin

回答

1

只要你替換選定的指數路徑之前,遍歷arrayForBool和每個項目設置爲NO。您可以持有一個屬性來存儲當前打開的部分索引,但除非您有數百個部分,否則不值得。

+0

我做到了,但重新加載表視圖的動畫看起來不錯。我已經更新了問題請檢查 –

+2

在循環中,構建已更改的部分索引的數組,然後重新加載它們。你不需要重新加載所有這些...... – Wain

+0

即使它被禁止,我只是給道路上的w,,先生,每當我找到你的答案,它已經死了精確你幫助了很多新手,謝謝! –