2010-09-02 31 views
0

这看起来很简单,但至今我找不到解决方案。用动画过滤UITableViewCells - iPhone开发

基本上我有两个选项分段控制。第一个是默认值(并在加载时自动显示),选中时显示表视图中的所有行。第二个是限制显示行的过滤器。这与iPhone手机应用程序的“最近”选项卡上使用的过滤“全部”和“未接”呼叫的设置完全相同。

目前我有两个不同的数组加载数据。问题是,当我交换数据时,没有动画表示行已被过滤。苹果已经在他们的手机应用程序中实现了这一点,但是我看不出如何实现这一点。

当用户在两种状态之间切换时,也许每个单元格都需要删除并重新添加 - 或者将我希望隐藏的单元格的高度设置为0会获得相同的效果?有没有人有任何生产这种手风琴式动画的经验?

我看了here的一些线索,但在滚动一些代码时遇到了问题。有没有人实施过这个?如果是这样,你是如何得到它的工作?

回答

1

您可以使用UITableViewRowAnimationFade动画在表格视图上调用deleteRowsAtIndexPaths:withRowAnimation:insertRowsAtIndexPaths:withRowAnimation:来实现类似的效果。

0

你看过reloadSections:withRowAnimation:吗?

基本的想法是调用reloadSections:withRowAnimation:并在UITableViewDataSource实现中切换分段控件的selectedSegmentIndex。

假设你的数据是平的(只有一节),这将是这个样子:

- (IBAction)segmentSwitch:(id)sender 
{ 
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch (self.segmentedControl.selectedSegmentIndex) 
    { 
     default: 
     case 0: 
      return [self.allRows count]; 
     case 1: 
      return [self.onlySomeRows count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    id data; 
    switch (self.segmentedControl.selectedSegmentIndex) 
    { 
     default: 
     case 0: 
      data = [self.allRows objectAtIndex:[indexPath row]]; 
      break; 
     case 1: 
      data = [self.onlySomeRows objectAtIndex:[indexPath row]]; 
      break; 
    } 

    //TODO: use data to populate and return a UITableViewCell... 
}