Tạo chế độ xem bảng và thanh tìm kiếm. Bạn phải thực hiện các đại biểu của họ UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate
.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1
{
searchBar.showsSearchResultsButton = YES;
searchBar.showsCancelButton = YES;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
//Implement some code
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1
{
searchBar.showsCancelButton = NO;
searchBar.showsSearchResultsButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText isEqualToString:@""]||searchText==nil){
[yourTable reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in yourArray)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
// NSRange r = [name rangeOfString:searchText];
// NSRange r = [name ];
if(r.location != NSNotFound)
{
//Implement the code.
}
counter++;
[pool release];
}
[yourTable reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar1
{
// if a valid search was entered but the user wanted to cancel, bring back the main list content
// Implement some code
@try{
[yourTable reloadData];
}
@catch(NSException *e){
}
[searchBar resignFirstResponder];
searchBar.text = @"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1
{
[searchBar1 resignFirstResponder];
}
Tôi đã cung cấp cho bạn các phương pháp không đầy đủ và bạn có thể thực hiện những gì bạn muốn làm.
Tôi nghĩ điều đó sẽ hữu ích cho bạn.
Nguồn
2012-06-28 04:40:26
Nhìn vào UISearchDisplayController. Nó thực sự là một tableview họ đã đọc từ một danh sách các thành phố. :) Liên kết với tài liệu của apple: [UISearchDisplayController] (http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html) Oh, và nếu tôi không nhầm bạn đang chạy iOS 6 beta tôi thấy. (Thanh điều hướng cho nó đi.) – erran
@ipwnstuff Oooops! Đã chỉnh sửa hình ảnh nhờ: P và bạn có biết cơ hội nào họ đang sử dụng không? – sridvijay
Tôi không biết nhưng @Greg Wang đã thêm một danh sách. – erran