gpt4 book ai didi

ios - 使用 tableview 中的搜索栏搜索数据

转载 作者:行者123 更新时间:2023-11-28 17:51:11 25 4
gpt4 key购买 nike

我是 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;
},

这是我的截图

enter image description here

这是我的代码

- (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;
}

我想用名字搜索。请帮帮我兄弟。谢谢你提前....

最佳答案

首先,将您的对象数组命名为 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 类的对象

一个。 arr临时表数据

arr表数据

然后在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;
}

关于ios - 使用 tableview 中的搜索栏搜索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com