2017-04-18 90 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