Add search bar and IOS table View to the table view of ios
The following figure shows the implementation result. This effect is modified based on the custom table view in the previous article.
# Import <UIKit/UIKit. h>
@ Interface IkrboyViewController4: UIViewController
{
NSArray * dataArr; // used to display table View data
NSArray * allDataArr; // stores all data and assigns the search result to dataArr through keyword search.
}
@ Property (weak, nonatomic) IBOutlet UISearchBar * searchBar;
@ End
# Import <UIKit/UIKit. h> @ interface IkrboyViewController4: UIViewController {NSArray * dataArr; // NSArray * allDataArr used to display table views; // all data is stored, assign the search result to dataArr} @ property (weak, nonatomic) IBOutlet UISearchBar * searchBar; @ end
3. Modify the custom method initTableViewData. Hiding ScopeBar is based on the iphone's display height. You can decide on your own.
Cpp Code
-(Void) initTableViewData {
NSBundle * bundle = [NSBundle mainBundle];
NSString * plistPath = [bundle pathForResource: @ "user_head" ofType: @ "plist"];
AllDataArr = [[NSArray alloc] initWithContentsOfFile: plistPath];
DataArr = [NSArray arrayWithArray: allDataArr];
NSLog (@ "table data count = % d", [allDataArr count]);
// Hide ScopeBar in the search bar
[Self. searchBar setShowsScopeBar: NO];
[Self. searchBar sizeToFit];
}
-(Void) initTableViewData {NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource: @ "user_head" ofType: @ "plist"]; allDataArr = [[NSArray alloc] initWithContentsOfFile: plistPath]; dataArr = [nsarraywitharray: allDataArr]; NSLog (@ "table data count = % d", [allDataArr count]); // set ScopeBar in the search bar to hide [self. searchBar setShowsScopeBar: NO]; [self. searchBar sizeToFit];}
4. Add three event triggers for SearchBar
Cpp Code
// The following three methods are used to implement the SearchBar search function:
// Send a reload message to the table View data source when the text content changes
-(BOOL) searchDisplayController :( UISearchDisplayController *) controller shouldReloadTableForSearchString :( NSString *) searchString
{
[Self filterContentForSearchText: searchString scope: self. searchBar. selectedScopeButtonIndex];
// If YES, the following table view can be reloaded.
Return YES;
}
// Send a reload message to the table View data source when the Scope Bar selects to send a change
-(BOOL) searchDisplayController :( UISearchDisplayController *) controller shouldReloadTableForSearchScope :( NSInteger) searchOption
{
[Self filterContentForSearchText: self. searchBar. text scope: searchOption];
// If YES, the following table view can be reloaded.
Return YES;
}
// Click the event of the cancel button
-(Void) searchBarCancelButtonClicked :( UISearchBar *) searchBar
{
// Query all
[Self filterContentForSearchText: @ "" scope:-1];
}
// The SearchBar search function is implemented in the following three methods // when the text content changes, send a reload message to the table view Data Source-(BOOL) searchDisplayController :( UISearchDisplayController *) controller shouldReloadTableForSearchString :( NSString *) searchString {[self filterContentForSearchText: searchString scope: self. searchBar. selectedScopeButtonIndex]; // In case of YES, return YES can be reloaded in the following Table view;} // send a reload message (BOOL) to the table View data source when the Scope Bar is changed) searchDisplayController :( UISearchDisplayController *) controller shouldReloadTableForSearchScope :( NSInteger) searchOption {[self filterContentForSearchText: self. searchBar. text scope: searchOption]; // In case of YES, the following table view can reload return YES;} // click the event of the cancel button-(void) searchBarCancelButtonClicked :( UISearchBar *) searchBar {// query all [self filterContentForSearchText: @ "" scope:-1];}
5. Custom keyword search
Cpp Code
// Custom search method: searches for elements that meet the search conditions from allDataArr Based on the keywords, and assigns the matching array to dataArr. Because dataArr is the data source of the table view, therefore, the record of the table view changes accordingly.
-(Void) filterContentForSearchText :( NSString *) searchText scope :( NSUInteger) scope;
{
If ([searchText length] = 0)
{
// Query all
DataArr = [NSArray arrayWithArray: allDataArr];
NSLog (@ "dataArr count = % d", [dataArr count]);
Return;
}
NSPredicate * scopePredicate;
Switch (scope ){
Case 0:
ScopePredicate = [NSPredicate predicateWithFormat: @ "(SELF. itemName contains [c] % @) OR (SELF. itemImagePath contains [c] % @)", searchText, searchText];
NSLog (@ "searchText = % @", searchText );
DataArr = [NSArray arrayWithArray: [allDataArr filteredArrayUsingPredicate: scopePredicate];
Break;
Case 1:
ScopePredicate = [NSPredicate predicateWithFormat: @ "SELF. itemName contains [c] % @", searchText];
DataArr = [NSArray arrayWithArray: [allDataArr filteredArrayUsingPredicate: scopePredicate];
Break;
Case 2:
ScopePredicate = [NSPredicate predicateWithFormat: @ "SELF. itemImagePath contains [c] % @", searchText];
DataArr = [NSArray arrayWithArray: [allDataArr filteredArrayUsingPredicate: scopePredicate];
Break;
}
}
// Custom search method: searches for elements that meet the search conditions from allDataArr Based on the keywords, and assigns the matching array to dataArr. Because dataArr is the data source of the table view, therefore, the record of the table view changes accordingly. -(Void) filterContentForSearchText :( NSString *) searchText scope :( NSUInteger) scope; {if ([searchText length] = 0) {// query all dataArr = [NSArray arrayWithArray: allDataArr]; NSLog (@ "dataArr count = % d", [dataArr count]); return;} NSPredicate * scopePredicate; switch (scope) {case 0: scopePredicate = [NSPredicate predicateWithFormat: @ "(SELF. itemName contains [c] % @) OR (SELF. itemImagePath contains [c] % @) ", searchText, searchText]; NSLog (@" searchText = % @ ", searchText); dataArr = [NSArray arrayWithArray: [allDataArr filteredArrayUsingPredicate: scopePredicate]; break; case 1: scopePredicate = [NSPredicate predicateWithFormat: @ "SELF. itemName contains [c] % @ ", searchText]; dataArr = [NSArray arrayWithArray: [allDataArr release: scopePredicate]; break; case 2: scopePredicate = [NSPredicate predicateWithFormat: @ "SELF. itemImagePath contains [c] % @ ", searchText]; dataArr = [NSArray arrayWithArray: [allDataArr filteredArrayUsingPredicate: scopePredicate]; break ;}}
6. Modify the cellForRowAtIndexPath method.
Cpp Code
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * CellIdentifier = @ "myTableCell ";
MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
// Add code begin: important, for showing searching results
// If the cell is not null, an error is returned if the cell corresponding to the identifier cannot be found during the search.
If (cell = nil ){
// The search result adopts the cell style in the simple table view, instead of the cell style in the custom table view.
Cell = [[MyTableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];
NSUInteger row = [indexPath row];
NSDictionary * rowDict = [dataArr objectAtIndex: row];
Cell. textLabel. text = [rowDict objectForKey: @ "itemName"];
NSString * imagePath = [rowDict objectForKey: @ "itemImagePath"];
Cell. imageView. image = [UIImage imageNamed: imagePath];
}
// Add code end
NSUInteger row = [indexPath row];
NSDictionary * rowDict = [dataArr objectAtIndex: row];
Cell. label. text = [rowDict objectForKey: @ "itemName"];
NSLog (@ "cell. label. text = % @", [rowDict objectForKey: @ "itemName"]);
NSString * imagePath = [rowDict objectForKey: @ "itemImagePath"];
Cell. image. image = [UIImage imageNamed: imagePath];
NSLog (@ "cell. image. image = % @", imagePath );
Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Return cell;
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "myTableCell"; MyTableViewCell * cell = [tableView progress: complete]; // add code begin: important, for showing searching results // if the cell is not null, an error is returned if the cell corresponding to the identifier cannot be found during the search. If (cell = nil) {// the search result adopts the cell style in the simple table view, instead of the style cell in the custom table view cell = [[MyTableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary * rowDict = [dataArr objectAtIndex: row]; cell. textLabel. text = [rowDict objectForKey: @ "itemName"]; NSString * imagePath = [rowDict objectForKey: @ "itemImagePath"]; cell. imageView. image = [UIImage imageNamed: imagePath];} // add code end NSUInteger row = [indexPath row]; NSDictionary * rowDict = [dataArr objectAtIndex: row]; cell. label. text = [rowDict objectForKey: @ "itemName"]; NSLog (@ "cell. label. text = % @ ", [rowDict objectForKey: @" itemName "]); NSString * imagePath = [rowDict objectForKey: @" itemImagePath "]; cell. image. image = [UIImage imageNamed: imagePath]; NSLog (@ "cell. image. image = % @ ", imagePath); cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell ;}