2016-02-19 2 views
1

i m new в IOS developer.my проблема заключается в том, что я хочу искать данные с использованием имени элемента. Когда я набираю B, а B - данные, это display.here это мое значение msmmable array.Поиск данных с помощью панели поиска из tableview

(
     { 
     counts = 0; 
     "drycleaning_price" = 10; 
     id = 2; 
     imagename = ""; 
     "iron_price" = 16; 
     name = Bedsheet; 
     "wash_price" = 15; 
     "washiron_price" = 14; 
    }, 
     { 
     counts = 0; 
     "drycleaning_price" = 12; 
     id = 3; 
     imagename = d; 
     "iron_price" = 16; 
     name = "Coat(Man)"; 
     "wash_price" = 14; 
     "washiron_price" = 13; 
    }, 
     { 
     counts = 0; 
     "drycleaning_price" = 15; 
     id = 4; 
     imagename = f; 
     "iron_price" = 10; 
     name = "Jeans(Man)"; 
     "wash_price" = 12; 
     "washiron_price" = 16; 
    }, 
     { 
     counts = 0; 
     "drycleaning_price" = 14; 
     id = 3; 
     imagename = ""; 
     "iron_price" = 12; 
     name = "Pants(Kid)"; 
     "wash_price" = 18; 
     "washiron_price" = 17; 
    }, 
     { 
     counts = 0; 
     "drycleaning_price" = 15; 
     id = 2; 
     imagename = s; 
     "iron_price" = 10; 
     name = "Pants(Man)"; 
     "wash_price" = 13; 
     "washiron_price" = 12; 
    }, 
     { 
     counts = 0; 
     "drycleaning_price" = 12; 
     id = 1; 
     imagename = ""; 
     "iron_price" = 32; 
     name = "Pillow Cover"; 
     "wash_price" = 30; 
     "washiron_price" = 15; 
    }, 

здесь это мой скриншот

enter image description here

здесь это мой код

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 100; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [self.searchResults count]; 

    } else { 
     return [arrayCounts count]; 
    } 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 
    customcell *cell = (customcell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil] objectAtIndex:0]; 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
      cell.textLabel.text = [[self.searchResults objectAtIndex:indexPath.row ]objectForKey:@"name"]; 
cell.txt_value.text = [NSString stringWithFormat:@"%@",self.searchResults[indexPath.row][@"counts"]]; 
cell.lbl_price.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"drycleaning_price"]]; 
     } else 
     { 


     cell.txt_value.text = [NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"counts"]]; 
     cell.lbl_title.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"name"]]; 
cell.lbl_price.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"drycleaning_price"]]; 
     } 
[cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged]; 
    return cell; 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 

    [self.searchResults removeAllObjects]; 

    NSPredicate *resultpredicate=[NSPredicate predicateWithFormat:@"SELF contains [cd] %@",self.searchResults]; 
    self.searchResults=[[arrayCounts valueForKey:@"name" ] filteredArrayUsingPredicate:resultpredicate]; 




    NSLog(@"searchlist %@",self.searchResults); 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 

я хочу найти с помощью name.pls помочь мне bro.thank вас заранее. ...

ответ

0

Прежде всего сделайте свой массив объектов именем ItemClass. Создание массива объектов сделает вашу жизнь легкой.

Создайте класс, унаследованный от класса NSObject.

Поместите это в Ном

@interface ItemClass : NSObject 

@property (strong, nonatomic) NSDate *count; 
@property (strong, nonatomic) NSString *name; 
@property (strong, nonatomic) NSString *dryCleanPrice; 

@end 

Затем хранить данные как этого

ItemClass *item = [[ItemClass alloc] init]; 
item.name [email protected]"yourName"; 
item.dryCleanPrice = @"334"; 
item.count = @"3"; 
[arrTempTableData addObject:item]; 

Затем использует две изменяемых массивы холдинговых объекты класса Item

а. arrTempTableData

b. arrTableData

Затем поместите следующий код в cellForRowAtIndexPath

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ static NSString *simpleTableIdentifier = @"SimpleTableItem"; 
customcell *cell = (customcell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) 
    cell = [[[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil] objectAtIndex:0]; 



ItemClass *item = arrTableData[indexPath.row]; 

    cell.txt_value.text = item.count; 
    cell.lbl_title.text=item.name; 
    cell.lbl_price.text=item.dryCleanPrice; 

    [cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged]; 
return cell; 
} 

затем в SearchBar

- (Недействительными) SearchBar: (UISearchBar *) SearchBar textDidChange: (NSString *) SearchText {

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@ ", searchText]; 
    self.arrTableData = [self.arrTempTableData filteredArrayUsingPredicate:predicate].mutableCopy; 
    self.arrTableData = self.arrTempTableData.count; 
    [self.tableView reloadData]; 
    if(searchText.length == 0){ 
     self.arrTableData = nil; 
     self.arrTableData = self.arrTempTableData.mutableCopy; 
    } 

}

+0

ok Я попробую бросить – iOS

+0

имя массива iw плохо хранить мой массив arrTempTableData yaa arrTableData – iOS

Смежные вопросы