我无法在 UITableView 上进行搜索。我已经在 UITableViewController h 文件中声明了委托等:
@protocol searchTanksListDelegate
- (void) searchTanksList:(UISearchBar *) sender textDidChange:(NSString *) searchText;
@end
@property (weak, nonatomic) id <searchTanksListDelegate> searchDelegate;
@property (weak, nonatomic) IBOutlet UISearchBar *searchTanksList;
我在 UITableViewController viewDidLoad 函数中将 searchTanksList.delegate 设置为 self。
我已经实现了这个方法:
- (void)searchTanksList:(UISearchBar *)searchTanksList textDidChange:(NSString *)searchText
{
if (searchText.length == 0)
{
self.isFiltered = false;
}
else {
self.isFiltered = true;
self.searchResults = [[NSMutableArray alloc] init];
for (tank* thisTank in self.tanks)
{
NSRange nameRange = [thisTank.tankProduct rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange numberRange = [thisTank.tankNumber rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange productNumberRange = [thisTank.tankProductNumber rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound || numberRange.location != NSNotFound || productNumberRange.location != NSNotFound)
{
[self.searchResults addObject:thisTank];
}
}
}
}
属性已经被合成等等 - 所以代码编译没有错误或警告......虽然我输入时没有调用 textDidChange 方法!