My app retrieves data in JSON format and displays it in a table view. I'm trying to implement a search method but the app crashes. Any ideas?
I can't seem to find any solutions that work.
Update:
- (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;
}
Your app crashes because productsArray
contains objects that are of type Product
not NSString.
So in your for loop change this:
for (NSString *str in productsArray)
into this
for (Product *product in productsArray)
Then get the NSString property of the product. I think you mean productName
您将两个不同的NSMutableArray分别用于retrieveData和searchBar,请尝试使用与在Table行和创建单元格中使用的相同的NSMutableArray。
It may have something to do with the method you are using to fetch the JSON. dataWithContentsOfURL: should not be used for network-based URLs. dataWithContentsOfURL:
To fetch data over the network, take a look at NSURLSession .
Where is it crashing exactly? I suggest putting in a break point and stepping through this code. That will probably be the easiest way to find the source.
You are filling the productsArray with objects type of Product
[productsArray addObject:[[Product alloc] initWithProdID:pID andProdName:pName andProdDescription:pDescription andProdImage:pImage andProdManufacturer:pManufacturer andProdQuantity:pQuantity andProdPrice:pPrice]];
And then searching for NSString in it: for (NSString *str in productsArray)
.
Correct way of searching would be when you create separate array containing list of items to be search. Eg Array of products name, or description.
Simple example to approach
@property (strong, nonatomic) NSMutableArray *filteredProductNameArray;
So here this property will store Array of product's name
- (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];
}
Here. And in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
You should check whether the name property of self.productsArray at indexPath.row equals to self.filteredProductNameArray;
I've left the further steps, but I guess you've got the idea and can complete by yourself. If you have any questions feel free to ask, I will try to help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.