2017-02-28 1 views
0

My AppはJSON形式のデータを取得し、テーブルビューで表示します。私は検索方法を実装しようとしていますが、アプリケーションがクラッシュします。何か案は?searchBarメソッドがNSMutableArray(Obj-C)で動作しない

解決方法が見つからないようです。

更新:

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

答えて

0

あなたが製品

[productsArray addObject:[[Product alloc] initWithProdID:pID andProdName:pName andProdDescription:pDescription andProdImage:pImage andProdManufacturer:pManufacturer andProdQuantity:pQuantity andProdPrice:pPrice]]; 

のオブジェクトタイプでproductsArrayを充填し、それに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のnameプロパティがself.filteredProductNameArrayに等しいかどうかをチェックする必要があります。

私はさらなる手順を進めましたが、あなたは考えを持っており、自分で完了することができます。お気軽にお問い合わせください

+0

はい、コードを投稿してください! – Cuckoo

+0

@Cuckooが答えを更新 –

+0

ありがとう、それは完璧に働いた! – Cuckoo

0

アプリがクラッシュproductsArrayはタイプProductないのNSStringのオブジェクトが含まれているため。

だからあなたのループの変更のために、この:この

for (Product *product in productsArray) 

for (NSString *str in productsArray) 

はその後、製品のNSStringのプロパティを取得します。あなたが意味すると思いますproductName

+0

retrieveDataメソッドがダウンすると、productArrayに文字列が入力されます:/ – Cuckoo

0

SEARCHDataとsearchBarに2つの異なるNSMutableArrayを使用しています。テーブル行で使用したものと同じものを使用して、セルを作成してみてください。

0

これは、JSONを取得するために使用しているメソッドと関係があります。 dataWithContentsOfURL:ネットワークベースのURLには使用しないでください。 dataWithContentsOfURL:

ネットワーク経由でデータを取得するには、NSURLSessionをご覧ください。

どこが正確にクラッシュしていますか?ブレークポイントを入れてこのコードを実行することをお勧めします。それはおそらくソースを見つける最も簡単な方法でしょう。

+0

検索バーで入力を開始するとクラッシュします:/ – Cuckoo

+0

未知のセレクタ 'NSInvalidArgumentException'、理由: ' - [Product rangeOfString:options:]:インスタンスに送信された認識できないセレクタ0x17026a840' – Cuckoo

関連する問題