点击空的UITableView以重新搜索搜索栏

时间:2009-11-11 04:13:47

标签: ios iphone uitableview uisearchbar

我的桌面视图顶部有一个搜索栏。当我点击搜索时,它会被第一响应者调用并出现键盘。搜索栏有一个取消按钮,但当表格为空时,我希望用户能够点击空表行的任何位置,以将搜索栏和键盘作为第一响应者重新签名。我该怎么做?

2 个答案:

答案 0 :(得分:4)

您必须创建自己的UITableView类来覆盖touchesEnded方法。然后你可以回调你的代表,它应该是你的视图控制器,并告诉它告诉你的UISearchBar辞职第一响应者。

UITableView派生类看起来像这样:

#import <UIKit/UIKit.h>

@interface FRTableView : UITableView {
}

@end

// ------------------------------

#import "FRTableView.h"

@implementation FRTableView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    NSLog(@"Touches ended");

    // Bubble back up to your view controller which should be this
    // table view's delegate.
    if ([self delegate] && [[self delegate] respondsToSelector:@selector(touchEnded)])
        [[self delegate] performSelector:@selector(touchEnded)];
}

@end

确保在Interface Builder中设置表视图以使用自定义类而不是普通的UITableView类。然后在你的视图控制器中,实现我称之为touchEnded的选择器,如下所示:

- (void)touchEnded;
{
    NSLog(@"Back in root view controller.");
    [searchBar resignFirstResponder];
}

其中searchBar是连接到Interface Builder中的UISearchBar的IBOutlet。

答案 1 :(得分:0)

你不试试...... UISearchDisplayController

只需在你的.h文件中定义这些

IBOutlet UISearchBar * mySearchBar; UISearchDisplayController * mySearchDisplayController; IBOutlet UITableView * myTableView;

并在.h

中处理UISearchBarDelegate

现在在.m文件中只需编写这些代码

mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];     mySearchDisplayController.searchResultsDelegate = self;     mySearchDisplayController.searchResultsDataSource = self;     mySearchDisplayController.delegate = self;

我希望这可能会有所帮助.......