2017-04-18 88 views
0

我有展開/摺疊選項時,用戶敲擊頁腳中的按鈕,但我不想表崩潰一路一個UITableView,我想很少細胞始終保持可見狀態,並從該點開始展開並摺疊至該點。我的表格視圖有三個部分,每個部分的可見單元格數目可以不同。當單擊自定義頁腳按鈕時,我可以一路展開並摺疊它,但正努力爲不同部分保留不同數量的可見單元格。 (我使用NIB和新的使用Objective-C) 在此先感謝,任何進一步的說明,請讓我知道。UITableView的多段展開/收起保持幾個細胞始終可見

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ 
    static NSString *cellIdentifier = @"FOOTER"; 
    CustomFootrCell *customFooter=[tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier]; 
//Section shoud be 'section' 
    [customFooter.expandButton setTag:section]; 
    [customFooter.expandButton addTarget:self action:@selector(expandButton:)forControlEvents:UIControlEventTouchUpInside]; 
    return customFooter; 
} 

-(void)expandButton: (id)sender{ 
    //Here all my conditons to decide if that section is expanded/collapsed 
    //And update it accordingly on numberOfRowsInSection method 
//Add begin and end for animation 
    [self.myTablebeginUpdates]; 

    [self.myTable reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.myTableendUpdates]; 
} 

回答

0

你幾乎知道了,我需要看看你是如何設置expandButton內的條件,當它被點擊。您可以設置應該有多少行是您numberOfRowsInSection 每一節像這個

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if(section==1){ 
     if(sectionOneExpanded==true){ 
      return totalNumberOfRowForThatSection_When_Expanded; 
     } 
     else{ 
      return Total_Number_Of_Row_TO_Keep_Visible_When_Collapse; 
     } 
    } 
    else if(section==2){ 
     if(sectionTwoExpanded==true){ 
      return totalNumberOfRowForThatSection_When_Expanded; 
     } 
     else{ 
      return Total_Number_Of_Row_TO_Keep_Visible_When_Collapse; 
     } 
    } 
    else if(section==3){ 
     if(sectionThreeExpanded==true){ 
      return totalNumberOfRowForThatSection_When_Expanded; 
     } 
     else{ 
      return Total_Number_Of_Row_TO_Keep_Visible_When_Collapse; 
     } 
    } 
} 

可見,可以爲expandButton這樣

-(void)expandButton: (id)sender{ 
    //Here you can do something like that 
    if([sender tag]==1){ 
     //Set a falg to see if the section is already expanded or not 
     SectionOneExpended=!SectionOneExpended; 
    } 
    else if([sender tag]==2){ 
     SectionTwoExpended=!SectionTwoExpended; 
    } 
    [self.myTable reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

我有git的一個簡單的例子,你可以檢查添加行爲this
希望它有幫助....

+0

非常感謝您的快速回復。我將嘗試一下,看看它是否有效。你的git示例項目是合法的:) – Tanvir