使用 tableview 中的搜索栏搜索数据
Posted
技术标签:
【中文标题】使用 tableview 中的搜索栏搜索数据【英文标题】:Search data using searchbar from tableview 【发布时间】:2016-02-19 10:06:45 【问题描述】:我是 ios 开发人员的新手。我的问题是我想使用项目名称搜索数据。当我键入 B 时,显示 B 开始数据。这是我的 nsmutable 数组值。
(
counts = 0;
"drycleaning_price" = 10;
id = 2;
imagename = "";
"iron_price" = 16;
name = Bedsheet;
"wash_price" = 15;
"washiron_price" = 14;
,
counts = 0;
"drycleaning_price" = 12;
id = 3;
imagename = d;
"iron_price" = 16;
name = "Coat(Man)";
"wash_price" = 14;
"washiron_price" = 13;
,
counts = 0;
"drycleaning_price" = 15;
id = 4;
imagename = f;
"iron_price" = 10;
name = "Jeans(Man)";
"wash_price" = 12;
"washiron_price" = 16;
,
counts = 0;
"drycleaning_price" = 14;
id = 3;
imagename = "";
"iron_price" = 12;
name = "Pants(Kid)";
"wash_price" = 18;
"washiron_price" = 17;
,
counts = 0;
"drycleaning_price" = 15;
id = 2;
imagename = s;
"iron_price" = 10;
name = "Pants(Man)";
"wash_price" = 13;
"washiron_price" = 12;
,
counts = 0;
"drycleaning_price" = 12;
id = 1;
imagename = "";
"iron_price" = 32;
name = "Pillow Cover";
"wash_price" = 30;
"washiron_price" = 15;
,
这是我的截图
这是我的代码
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 100;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if (tableView == self.searchDisplayController.searchResultsTableView)
return [self.searchResults count];
else
return [arrayCounts count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"SimpleTableItem";
customcell *cell = (customcell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
cell = [[[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil] objectAtIndex:0];
if (tableView == self.searchDisplayController.searchResultsTableView)
cell.textLabel.text = [[self.searchResults objectAtIndex:indexPath.row ]objectForKey:@"name"];
cell.txt_value.text = [NSString stringWithFormat:@"%@",self.searchResults[indexPath.row][@"counts"]];
cell.lbl_price.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"drycleaning_price"]];
else
cell.txt_value.text = [NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"counts"]];
cell.lbl_title.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"name"]];
cell.lbl_price.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"drycleaning_price"]];
[cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged];
return cell;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
[self.searchResults removeAllObjects];
NSPredicate *resultpredicate=[NSPredicate predicateWithFormat:@"SELF contains [cd] %@",self.searchResults];
self.searchResults=[[arrayCounts valueForKey:@"name" ] filteredArrayUsingPredicate:resultpredicate];
NSLog(@"searchlist %@",self.searchResults );
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
我想用名字搜索。请帮我兄弟。谢谢你的提前......
【问题讨论】:
【参考方案1】:首先将你的对象数组命名为ItemClass。 制作一个对象数组会让你的生活变得轻松。
创建一个继承自 NSObject 类的类。
把这个放进去
@interface ItemClass : NSObject
@property (strong, nonatomic) NSDate *count;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *dryCleanPrice;
@end
然后像这样存储你的数据
ItemClass *item = [[ItemClass alloc] init];
item.name =@"yourName";
item.dryCleanPrice = @"334";
item.count = @"3";
[arrTempTableData addObject:item];
然后使用两个可变数组保存Item类的对象
一个。 arrTempTableData
b. arrTableData
然后将以下代码放入 cellForRowAtIndexPath
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"SimpleTableItem";
customcell *cell = (customcell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
cell = [[[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil] objectAtIndex:0];
ItemClass *item = arrTableData[indexPath.row];
cell.txt_value.text = item.count;
cell.lbl_title.text=item.name;
cell.lbl_price.text=item.dryCleanPrice;
[cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged];
return cell;
然后在搜索栏中
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@ ", searchText];
self.arrTableData = [self.arrTempTableData filteredArrayUsingPredicate:predicate].mutableCopy;
self.arrTableData = self.arrTempTableData.count;
[self.tableView reloadData];
if(searchText.length == 0)
self.arrTableData = nil;
self.arrTableData = self.arrTempTableData.mutableCopy;
【讨论】:
我将使用哪个数组名来存储我的数组 arrTempTableData yaa arrTableData以上是关于使用 tableview 中的搜索栏搜索数据的主要内容,如果未能解决你的问题,请参考以下文章
当使用 swift 中的搜索栏在 tableview 上选择一行时,它不会执行所需的功能