我在IOS developer.my中的新问题是我想使用项目名称搜索数据。当我输入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;
}
我想使用name.pls搜索帮助我,感谢你提前....
答案 0 :(得分:-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
湾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;
}
然后在searchBar
- (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;
}
}