2012-04-18 55 views
2

我想展开UITableView部分,我必须显示UITableViewCells。我该怎么做才能做到这一点?想扩大和缩小UITableview部分

+2

Google'expandable table view iphone'我有一个视频链接+ 5相关的SO问题,请做一个搜索。谢谢 – iNoob 2012-04-18 06:51:54

回答

6
  • 一个简单的实现方法是将 部分的单元高度保持为零。
  • 充分利用viewForSectionHeader可触摸
  • 当你触摸它,下段
  • 细胞设定适当的高度写逻辑部分

OR之间切换,

  • 在触摸节标题重新加载表中的更新的行计数部分触摸。

许多其他方式来做到这一点。 Apple example

+0

谢谢Vignesh。我正在尝试。如果你有其他简单的例子,那么对我来说 – Nishi 2012-04-18 06:53:36

+0

@ user1335760会是个好主意。看看苹果的例子,它做你想做的。 – Vignesh 2012-04-18 06:55:55

+0

+1链接到Apple示例 – onmyway133 2013-11-26 07:42:12

0

根据Vignesh的回答,我已经尝试了第二种解决方案。“在触摸节标题时,用更新后的行重新载入表计数为触摸的节。

首先,声明一个数组来存储每个section的isExpanded标签。初始的,所有的值都是BOOL NO.Means所有的section行被折叠。

然后,在

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

方法,通过实施下面的代码sectionHeader触摸事件: 因为我使用作为区段标题视图自定义单元格。所以这里是“细胞”,你可以使用你自己的观点。

​​

而且,在截取sectionHeader时做些事情。

- (void)sectionTapped:(UITapGestureRecognizer *)recognizer 
{ 

    NSMutableArray *isSectionTouched=[[NSMutableArray alloc]initWithCapacity:_sectionExpandBool.count]; 
    isSectionTouched=[_sectionExpandBool mutableCopy]; 
    if ([[isSectionTouched objectAtIndex:recognizer.view.tag]boolValue]==YES) { 
     [isSectionTouched replaceObjectAtIndex:recognizer.view.tag withObject:[NSNumber numberWithBool:NO]]; 
    }else if ([[isSectionTouched objectAtIndex:recognizer.view.tag]boolValue]==NO){ 
     [isSectionTouched replaceObjectAtIndex:recognizer.view.tag withObject:[NSNumber numberWithBool:YES]]; 
    } 
    _sectionExpandBool=isSectionTouched; 
    [self.tableView reloadData]; 

} 

不要忘记修改numberOfRowsInSection方法。 row.count应根据_sectionExpandBool的值进行更改,如果该部分的ExpandBool为YES,则应返回正确的数据源编号,否则返回0.

它符合我的期望。不过,我有点担心内存泄漏或什么,因为每次点击标题时,整个表格视图都会重新加载。

我不知道是否有一些解决方案只是重新加载特定的部分。谢谢。