2017-10-28 112 views
2

我已經實現了一個帶有兩個標籤的自定義單元格的TableView來填充城市名稱和城市ID(我保持城市ID標籤隱藏)。在這裏,我的問題是當我搜索城市名稱時,我也無法獲得城市ID,當我搜索城市名稱時,我希望這兩個值都被過濾。如何使用Obejctive C在tableview單元格中搜索特定值時獲取所有數組值?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *[email protected]"citylocation"; 
     searchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
     if (!cell) { 
      cell= [[searchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
     } 
     if(!isFilltered) 
     { 
      cell.textLabel.text = [avahotel objectAtIndex:indexPath.row]; 
      cell.city.text = [[createdDate objectAtIndex:indexPath.row]stringValue]; 
     }else 
     { 
      cell.textLabel.text = [filteredString objectAtIndex:indexPath.row]; 
    }  
    return cell; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(isFilltered) 
    { 
     return [filteredString count]; 
    } 
    return [avahotel count]; 
} 
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if(searchText.length == 0) 
    { 
     isFilltered = NO; 
    }else 
    { 
     isFilltered = YES; 
     filteredString = [[NSMutableArray alloc]init]; 
     for(NSString *str in avahotel) 
     { 
      NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

      if(stringRange.location != NSNotFound) 
      { 
       [filteredString addObject:str]; 
      } 
     } 
    } 
    [_searchtableview reloadData]; 
} 
+0

我有一個問題在這裏。你想在filteredString數組中獲得關於城市ID的信息嗎?當真正搜索城市名稱?還是我誤解了? – Elena0987

+0

搜索城市名稱和所有工作正常,我的需要是,如何過濾這兩個標籤。例如,如果我正在搜索欽奈的手段,我希望欽奈和城市id(100)值。 –

回答

0

在該方法的其他部分使用以下代碼。搜索欄textDidChange

更新

isFilltered = YES; 
    filteredString = [[NSMutableArray alloc]init]; 
    filteredCityId = [[NSMutableArray alloc]init]; 

    for(Int i=0; i<avahotel.count; i++) 
    { 
     NSString *str = [avahotel objectAtIndex:i]; 
     NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if(stringRange.location != NSNotFound) 
     { 
      [filteredString addObject:[avahotel objectAtIndex:i]]; 
      NSString *strId = [NSString stringWithFormat:@"%d",i]; 
      [filteredCityId addObject:strId] 

      // here your both filter array declare.filteredcityId and filteredString   
     } 
    } 
+0

這個類別不是關鍵值編碼兼容的關鍵cityname ...我得到這個錯誤.avahotel是我的數組,其中包含所有cityname –

+0

你可以在這裏分享avahotel(「打印數據響應」)數組?所以我會得到解決這個錯誤。 –

+0

可用酒店( AGRA, 班加羅爾 奈 GOA, HYDERABAD, JAIPUR, KOLKATA, 「MUMBAI/BOMBAY」, 「新德里」, 阿加爾塔拉, AHMEDABAD, 艾藻爾, 阿杰梅爾, 阿科拉, 阿里巴格, ALLAHABAD, 阿勒皮, 阿爾莫拉, 阿拉斯索哈維, 阿爾瓦爾, 安巴拉, AMLA, AMRITSAR, 阿南德 ANKLESWAR, ARONDA, ASHTAMUDI, } –

相關問題