1

我只需要了解如何在 iPhone/iPad 中实现“下拉​​搜索”功能。我有一个 UITableView,当我在 tableView 的开头并向下滚动时,我想显示一个 searchBar(在启动时隐藏)。然后滚动到 tableView 的底部,我想隐藏 searchBar。我在互联网上查看了一些想法和可能的解决方案,但没有成功。当你向下滚动 tableView 时,我需要一些类似的东西。

4

3 回答 3

2

从这个线程:stackoverflow

此解决方案适用于 ViewController.m 文件:

#import "ViewController.h"

@interface ViewController() <UITableViewDelegate>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) UIView *viewSearch;
@property (nonatomic) UITextField *txtSearch;

@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];

    // view
    [self.view setBackgroundColor:[UIColor lightGrayColor]];

    // tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 300)];
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];

    // search view
    self.viewSearch = [[UIView alloc]initWithFrame:CGRectMake(0, -50, self.view.frame.size.width, 50)];
    [self.viewSearch setBackgroundColor:[UIColor lightGrayColor]];
    [self.tableView addSubview:self.viewSearch];

    // search textfield
    self.txtSearch = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width-40, 30)];
    [self.txtSearch setBackgroundColor:[UIColor whiteColor]];
    [self.txtSearch setPlaceholder:@"Search..."];
    [self.viewSearch addSubview:self.txtSearch];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate) {
        [self.txtSearch resignFirstResponder];
    }

    if(scrollView.contentOffset.y < 0)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];

        self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
        [self.txtSearch becomeFirstResponder];

        [UIView commitAnimations];
    } else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];

        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [self.txtSearch setText:@""];

        [UIView commitAnimations];
    }
}
@end
于 2014-09-15T14:39:17.067 回答
0

在您的控制器中实现scrollViewDidScroll委托功能,并在用户将表格滚动到具有负 y 坐标的内容偏移量时显示搜索栏。

表格视图必须启用反弹才能正常工作。

于 2013-10-28T15:59:19.543 回答
0

如果您使用 aUITableViewController来实现您的功能,您可以使用 aUIRefreshControl来实现您想要的“下拉搜索”行为。表格视图控制器有一个refreshControl可以分配给它的属性。

希望这可以帮助!

于 2013-10-28T16:05:38.960 回答