5

我有一个 UITableViewController 的子类。我有代码可以向/从我的 tableView 的 tableHeaderView 添加/删除 UISearchBar。这是我必须执行这些任务的代码:

self.tableView.tableHeaderView = uiSearchBar; //Make the search bar appear
self.tableView.tableHeaderView = nil; //Make the search bar disappear

问题是我希望 UISearchBar 的添加/删除是动画的;添加时从顶部滑入视图,然后在删除时向上滑动并移出视图,而不仅仅是出现和消失。有什么建议么?

谢谢。

4

3 回答 3

7

我终于能够弄清楚这一点。结果很简单。我没有将 UISearchBar 添加到表头,而是将其插入到带有动画 UITableViewRowAnimationTop 的表的第一个单元格中,并使用相同的方法将其删除。这导致酒吧从顶部滑入和滑出。这是使栏出现的代码:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.baseUiTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.baseUiTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

这是删除搜索栏的代码:

[uiSearchBar resignFirstResponder];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.baseUiTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
于 2009-08-28T04:24:45.180 回答
4

也适用于 UITableViewStyleGrouped 的更简单的方法(另外不需要更改行数)是为contentInset表格设置动画:

CGFloat h = uiSearchBar.bounds.size.height;
UITableView *tv = self.tableView;
if (tv.tableHeaderView)
{  // hide bar
   [UIView animateWithDuration:0.3 animations:^{
      tv.contentInset = UIEdgeInsetsMake(-h, 0, 0, 0);
   } completion:^(BOOL finished) {
      tv.contentInset = UIEdgeInsetsZero;
      tv.tableHeaderView = nil;
      [tv scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
   }];
}
else
{  // show bar
   uiSearchBar.frame = CGRectMake(0, -h, tv.frame.size.width, h);
   [UIView animateWithDuration:0.3 animations:^{
      [tv scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
      tv.tableHeaderView = uiSearchBar;
   }];
}

我知道这是在 OP 提出问题三年后,但由于这是实现预期效果的好选择,我认为它可能对其他遇到相同问题的人有用。

于 2012-05-02T03:47:39.857 回答
0

尝试让您的表格视图滚动(动画),以便只有搜索栏下方的行可见,然后移除搜索栏并滚动到没有动画的同一行。

于 2009-08-25T17:20:16.993 回答