簡體   English   中英

searchBar方法不適用於我的NSMutableArray(Obj-C)

[英]searchBar method doesn't work with my NSMutableArray (Obj-C)

我的應用程序以JSON格式檢索數據,並將其顯示在表格視圖中。 我正在嘗試實現搜索方法,但應用程序崩潰。 有任何想法嗎?

我似乎找不到任何有效的解決方案。

更新:

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

您將兩個不同的NSMutableArray分別用於retrieveData和searchBar,請嘗試使用與在Table行和創建單元格中使用的相同的NSMutableArray。

它可能與您用來獲取JSON的方法有關。 dataWithContentsOfURL:不應用於基於網絡的URL。 dataWithContentsOfURL:

要通過網絡獲取數據,請查看NSURLSession

它到底在哪里崩潰? 我建議設置一個斷點並逐步執行此代碼。 這可能是找到來源的最簡單方法。

您正在用產品的對象類型填充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的name屬性是否等於self.filteredProductNameArray;

我已經離開了進一步的步驟,但是我想您已經有了主意,可以自己完成。 如果您有任何疑問,歡迎聯系我們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM