任何人都可以帮助分享iOS 8或更高版本的表搜索功能的示例代码,因为不推荐使用searchDisplayController
答案 0 :(得分:0)
下载this链接中提供的示例项目。 希望有所帮助。它演示了如何使用基于tableView搜索的UISearchController类。
答案 1 :(得分:0)
我发现这个" UISearchDisplayController已被UISearchController取代"。所以让我们试试UISearchController。
答案 2 :(得分:0)
#import“ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
{
NSMutableArray *tableData;
NSMutableArray *tableArray;
NSMutableArray *dummyArray;
NSMutableArray *searchArray;
NSString *searchTextString;
}
- (void)viewDidLoad {
[super viewDidLoad];
//set the selector to the text field in order to change its value when edited
[self.text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//here you set up the methods to search array and reloading the tableview
[self setupData];
[self updateSearchArray];
[self.tableView reloadData];
// Do any additional setup after loading the view, typically from a nib.
_tableView.allowsSelection=NO;
self.text.delegate=self;
}
//setting up the data sourch for the mutable array
- (void) setupData {
dummyArray = [[NSMutableArray alloc] init];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Values:", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Apple", @"name" , @"image1.JPG", @"image" , @"dummy 2 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy", @"name" , @"image1.JPG", @"image" , @"dummy 3 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mango", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Brown", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"jumps", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"hello", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"shahzaib", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"textFieldShouldBeginEditing");
textField.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
[_tableView setHidden:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if ([_text.text isEqualToString:@""] ||[_text.text isEqualToString:@" "])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Warning" message:@"TextField is empty now!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:_text.text, @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[_tableView reloadData];
searchTextString = textField.text;
[self updateSearchArray];
}
return YES;
}
#pragma - mark TextField Delegate Methods
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[_tableView setHidden:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [searchArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( cell == nil ){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[searchArray objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[self performSegueWithIdentifier:@"DummyDetail" sender:[NSNumber numberWithInt:indexPath.row]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
self.text.text=[[searchArray objectAtIndex:indexPath.row] objectForKey:@"name"];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Search Methods
-(void)textFieldDidChange:(UITextField*)textField
{
searchTextString = textField.text;
[self updateSearchArray];
}
//update seach method where the textfield acts as seach bar
-(void)updateSearchArray
{
if (searchTextString.length != 0)
{
_tableView.allowsSelection=YES;
searchArray = [NSMutableArray array];
for ( NSDictionary* item in dummyArray )
{
if ([[[item objectForKey:@"name"] lowercaseString] rangeOfString:[searchTextString lowercaseString]].location != NSNotFound)
{
[searchArray addObject:item];
}
else
{
//NSString *notfound;
//notfound=@"not found";
//[searchArray addObject:notfound];
}
}
}
else
{
searchArray = dummyArray;
_tableView.allowsSelection=NO;
}
[self.tableView reloadData];
}
它是一个完整的搜索,我认为这个可以帮助你,告诉我你是否需要更多的帮助...