2017-02-28 6 views
0

내 앱은 JSON 형식의 데이터를 검색하여 테이블보기로 표시합니다. 검색 방법을 구현하려고하지만 앱이 다운됩니다. 어떤 아이디어?내 NSMutableArray (Obj-C)와 searchBar 메서드가 작동하지 않습니다.

작동하는 솔루션을 찾을 수없는 것 같습니다.

업데이트 :

- (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의 이름 속성이 self.filteredProductNameArray에와 같은지 여부를 확인해야합니다;

나는 더 많은 단계를 밟았지만 아이디어를 얻었으며 스스로 완료 할 수 있다고 생각합니다.자유롭게 물어 보는 질문이 있으면 언제든지 도와 드리겠습니다.

+0

예, 코드를 게시하십시오! – Cuckoo

+0

@Cuckoo 님이 답변을 –

+0

고맙습니다. 완벽하게 작동했습니다. – Cuckoo

0

앱 충돌 productsArray이있는 NSString 형태가 아닌 Product의 개체를 포함하고 있기 때문에.

그래서 루프에 대한 변경 사항이있는 :

for (NSString *str in productsArray) 

for (Product *product in productsArray) 

에 그런 제품의는 NSString 속성을 얻을. 내 생각 엔 productName

+0

retrieveData 메소드가 다운 인 경우 productArray는 문자열로 채워집니다./ – Cuckoo

0

두 개의 다른 NSMutableArray를 retrieveData와 searchBar로 사용하고 있습니다. 테이블 행에서 사용했던 것과 동일한 것을 사용하고 셀을 생성 해보십시오.

0

JSON을 가져 오는 데 사용하는 방법과 관련이있을 수 있습니다. dataWithContentsOfURL : 네트워크 기반 URL에 사용하면 안됩니다. dataWithContentsOfURL:

네트워크를 통해 데이터를 가져 오려면 NSURLSession을 살펴보십시오.

정확히 충돌하는 부분은 어디입니까? 나는 중단 점을 넣고이 코드를 단계적으로 진행하는 것이 좋습니다. 아마 소스를 찾는 가장 쉬운 방법 일 것입니다.

+0

검색 창에 입력을 시작하면 충돌이 발생합니다./ – Cuckoo

+0

uncaught exception 'NSInvalidArgumentException', 이유 : '- [Product rangeOfString : options :] : 인식 할 수없는 selector가 인스턴스 0x17026a840에 전송되었습니다. – Cuckoo