searchBar 方法不适用于我的 NSMutableArray (Obj-C)
searchBar method doesn't work with my NSMutableArray (Obj-C)
我的应用程序以 JSON 格式检索数据并将其显示在 table 视图中。我正在尝试实施搜索方法,但应用程序崩溃了。有什么想法吗?
我似乎找不到任何有效的解决方案。
更新:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (!isFiltered) {
Product *productObject;
productObject = [productsArray objectAtIndex:indexPath.row];
cell.textLabel.text = productObject.prodName;
//Accessory.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
Product *productObject;
productObject = [self.filteredProductNameArray objectAtIndex:indexPath.row];
//Accessory.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
您的应用程序崩溃,因为 productsArray
包含类型为 Product
而非 NSString 的对象。
所以在你的 for 循环中改变这个:
for (NSString *str in productsArray)
进入这个
for (Product *product in productsArray)
然后获取商品的NSString属性。我想你的意思是 productName
您正在为 retrieveData 和 searchBar 使用两个不同的 NSMutableArray,请尝试使用您在 Table 行和创建单元格中使用的相同内容。
这可能与您用来获取 JSON 的方法有关。 dataWithContentsOfURL:不应用于基于网络的 URL。 dataWithContentsOfURL:
要通过网络获取数据,请查看 NSURLSession。
具体崩溃在哪里?我建议放置一个断点并逐步执行此代码。这可能是找到来源的最简单方法。
您正在用 Product
的对象类型填充 productsArray
[productsArray addObject:[[Product alloc] initWithProdID:pID andProdName:pName andProdDescription:pDescription andProdImage:pImage andProdManufacturer:pManufacturer andProdQuantity:pQuantity andProdPrice:pPrice]];
然后在里面搜索NSString:for (NSString *str in productsArray)
.
正确的搜索方式是创建包含要搜索的项目列表的单独数组。例如,产品名称或描述数组。
简单的方法示例
@property (strong, nonatomic) NSMutableArray *filteredProductNameArray;
所以这里属性将存储产品名称数组
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSMutableArray *productsNameArray = [[NSMutableArray alloc] init];
if (searchText.length == 0) {
isFiltered = NO;
self.filteredProductNameArray = self.productsArray; //If no str in textfield you should display all the data
}
else {
isFiltered = YES;
for (Product *product in self.productsArray) { //Because you have array type of Product, not NSString
NSString *productName = product.name; //Or method to access `name` property of the Product class
NSRange stringRange = [productName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location != NSNotFound) {
[productsNameArray addObject:productName];
}
}
}
self.filteredProductNameArray = productsNameArray;
// Here you got array of products name which matches string in search bar textfield. And it is the actual products name list to be displayed
[self.tableView reloadData];
}
这里。在你的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
您应该检查 indexPath.row 的 self.productsArray 的名称 属性 是否等于 self.filteredProductNameArray;
我已经离开了进一步的步骤,但我想你已经有了想法并且可以自己完成。如果您有任何问题,请随时提出,我会尽力提供帮助
我的应用程序以 JSON 格式检索数据并将其显示在 table 视图中。我正在尝试实施搜索方法,但应用程序崩溃了。有什么想法吗?
我似乎找不到任何有效的解决方案。
更新:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (!isFiltered) {
Product *productObject;
productObject = [productsArray objectAtIndex:indexPath.row];
cell.textLabel.text = productObject.prodName;
//Accessory.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
Product *productObject;
productObject = [self.filteredProductNameArray objectAtIndex:indexPath.row];
//Accessory.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
您的应用程序崩溃,因为 productsArray
包含类型为 Product
而非 NSString 的对象。
所以在你的 for 循环中改变这个:
for (NSString *str in productsArray)
进入这个
for (Product *product in productsArray)
然后获取商品的NSString属性。我想你的意思是 productName
您正在为 retrieveData 和 searchBar 使用两个不同的 NSMutableArray,请尝试使用您在 Table 行和创建单元格中使用的相同内容。
这可能与您用来获取 JSON 的方法有关。 dataWithContentsOfURL:不应用于基于网络的 URL。 dataWithContentsOfURL:
要通过网络获取数据,请查看 NSURLSession。
具体崩溃在哪里?我建议放置一个断点并逐步执行此代码。这可能是找到来源的最简单方法。
您正在用 Product
的对象类型填充 productsArray[productsArray addObject:[[Product alloc] initWithProdID:pID andProdName:pName andProdDescription:pDescription andProdImage:pImage andProdManufacturer:pManufacturer andProdQuantity:pQuantity andProdPrice:pPrice]];
然后在里面搜索NSString:for (NSString *str in productsArray)
.
正确的搜索方式是创建包含要搜索的项目列表的单独数组。例如,产品名称或描述数组。
简单的方法示例
@property (strong, nonatomic) NSMutableArray *filteredProductNameArray;
所以这里属性将存储产品名称数组
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSMutableArray *productsNameArray = [[NSMutableArray alloc] init];
if (searchText.length == 0) {
isFiltered = NO;
self.filteredProductNameArray = self.productsArray; //If no str in textfield you should display all the data
}
else {
isFiltered = YES;
for (Product *product in self.productsArray) { //Because you have array type of Product, not NSString
NSString *productName = product.name; //Or method to access `name` property of the Product class
NSRange stringRange = [productName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location != NSNotFound) {
[productsNameArray addObject:productName];
}
}
}
self.filteredProductNameArray = productsNameArray;
// Here you got array of products name which matches string in search bar textfield. And it is the actual products name list to be displayed
[self.tableView reloadData];
}
这里。在你的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
您应该检查 indexPath.row 的 self.productsArray 的名称 属性 是否等于 self.filteredProductNameArray;
我已经离开了进一步的步骤,但我想你已经有了想法并且可以自己完成。如果您有任何问题,请随时提出,我会尽力提供帮助