我从昨天开始尝试实施UISearchBar
来过滤(搜索)我的UITableView
,但没有运气。
我将UISearchBar
添加到我的头文件中,如下所示:
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController<UISearchBarDelegate>{
}
@property (weak, nonatomic) IBOutlet UITableView *TableView;
@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar;
@property (nonatomic, strong) NSMutableArray *filteredsarkilar;
@property BOOL isFiltered;
@end
我将UITableView
的数据源设为NSDictionary
,其索引和部分包含标题和副标题。它完全按我想要的方式工作。这是我已经尝试实现搜索功能的实现文件。顺便说一句,当我按下搜索按钮时键盘不会消失,为什么?
#import "TableViewController.h"
#import "DetailViewController.h"
@interface TableViewController (){
NSDictionary *sarkilar;
NSArray *sarkilarSectionTitles;
NSArray *sarkilarIndexTitles;
}
@end
@implementation TableViewController
@synthesize mySearchBar, filteredsarkilar, isFiltered;
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:newButton];
sarkilar = @{
@"A" : @[@{@"title":@"Alayına İsyan",@"subtitle":@"Seslendiren: Mustafa Sandal"},
@{@"title":@"Ardindan",@"subtitle":@"Seslendiren: Sinasi Gurel"}],
@"B" : @[@{@"title":@"Birak Gitsin",@"subtitle":@"Seslendiren: Tarkan"},
@{@"title":@"Buralar",@"subtitle":@"Seslendiren: Duman"}],
@"C" : @[@{@"title":@"Cephaneler",@"subtitle":@"Seslendiren: Burak Kut"},
@{@"title":@"Cari Acik",@"subtitle":@"Seslendiren: Kristal"}],
};
sarkilarSectionTitles = [[sarkilar allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
sarkilarIndexTitles = @[@"A", @"B", @"C",@"Ç", @"D", @"E", @"F", @"G", @"H", @"I",@"İ", @"J", @"K", @"L", @"M", @"N", @"O", @"Ö", @"P", @"R", @"S",@"Ş", @"T", @"U",@"Ü", @"V", @"Y", @"Z"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sarkilarSectionTitles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (isFiltered == YES) {
return filteredsarkilar.count;
}
else
{
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:section];
NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
return [sectionSarkilar count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sarkilarSectionTitles objectAtIndex:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
NSDictionary *dict = [sectionSarkilar objectAtIndex:indexPath.row];
NSString *title = [dict objectForKey:@"title"];
NSString *subtitle = [dict objectForKey:@"subtitle"];
if (isFiltered == YES) {
cell.textLabel.text = title;
cell.detailTextLabel.text = subtitle;
}
else
{
cell.textLabel.text = title;
cell.detailTextLabel.text = subtitle;
}
CGSize itemSize = CGSizeMake(50, 50);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return sarkilarIndexTitles;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [sarkilarSectionTitles indexOfObject:title];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0)
{
isFiltered = NO;
}
else
{
isFiltered =YES;
filteredsarkilar = [[NSMutableArray alloc]init];
for (NSString *title in sarkilar)
{
NSRange titleRange = [title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleRange.location != NSNotFound)
{
[filteredsarkilar addObject:title];
}
}
}
[TableView reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[mySearchBar resignFirstResponder];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *destinationViewController = segue.destinationViewController;
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
destinationViewController.title = selectedCell.textLabel.text;
}
}
@end
如果可以,请帮助我,因为我一直在把头发拉开!谢谢