【问题标题】:how to search data from tableView in ipad using textfield如何使用文本字段从 ipad 中的 tableView 中搜索数据
【发布时间】:2013-08-19 06:55:33
【问题描述】:

我有 ipad,它有 tableView 显示内容,我希望当用户在搜索文本字段中输入任何数据时,匹配的记录应该显示在 tableView 中。

- (void)onSearchButtonClick 
 {   

 if (searchTextField.text == nil || searchTextField.text.length == 0) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You must fill the text to search first!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    return;
}

  [searchTextField resignFirstResponder];
}

这是tableView中通常显示的内容

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




   static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

       CustomCell *cell = (CustomCell *)[tableView 

                                  d      equeueReusableCellWithIdentifier: CustomCellIdentifier];

  if (cell == nil)  
      {


    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" 
                                                 owner:self options:nil];
    for(id oneObject in nib)
        if ([oneObject isKindOfClass:[CustomCell class]])
            cell = (CustomCell *)oneObject;

    cell.selectionStyle=UITableViewCellSelectionStyleNone;


}


      appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

ObjectData*obj;


    obj=[appDelegate.detailArray objectAtIndex:indexPath.row];



type=obj.contentType;

content_Type=obj.contentType;

content_Source=obj.contentSource;
    cell.text=Content_Source;


   return cell;
  }

我希望当点击搜索时它应该匹配记录并显示在 tableView 中

【问题讨论】:

    标签: ios search tableview


    【解决方案1】:

    在 onSearchButtonClick 方法中编写这段代码

      NSString *firstLetter = @"";
      NSInteger strlen=[textfield.text length];
      for (NSString *ABCValue in ABCArray)
     {
        if(ABCValue.length >= stringlen)
               firstLetter = [ABCValue substringToIndex:stringlen];
        if([textfield.text.uppercaseString isEqualToString:firstLetter.uppercaseString])
        { 
                [tableArray addObject:ABCValue]; //Store it in NSMutableArray
            break;
        }
      }
    

    【讨论】:

    • ABCArray 是数组,它将在其中搜索显示表数据的平均数组
    • 是的,ABCArray 是一个在 numberofRowsInTableView 方法中使用的数组。
    • 为什么我们使用 firstLetter empty
    • 在进入循环之前,我们正在清除之前存储的文本。在循环中,它充满了一些单词。没有必要专注于这一点。只需检查您是否正在获取数据
    【解决方案2】:

    首先,您应该使用另一个可变数组,而不是使用 appdelegate 数组填充表数据,该数组将在开始时包含 appdelegate 数组的副本。 然后在您点击搜索按钮的方法中,您必须编写以下代码以在表格中显示结果。

    //Allocate a new array....
    NSMutableArray *newArray = [[[NSMutableArray alloc] init] autorelease];
    
    //The search query....
    NSString strQuery=[textfield.text length];
    
    //Go through all the array for populating the table data.....
    for (ObjectData *obj in appDelegate.detailArray)
    {
        //Check for the tye of the object.....
        if ([obj isKindOfClass:[ObjectData class]])
        {
            //Now the contentSource of the object contains the search query then add the object to new array....
            if([obj.contentSource rangeOfString:strQuery options:NSCaseInsensitiveSearch].location != NSNotFound)
            {
                [newArray addObject:obj];
            }
        }
    }
    
    //Dont forget to populate the table with new array.....
    yourArrayForpopulatingData = [newArray mutableCopy];
    //after completion reload the tableview....
    [aTableView reloadData];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-04
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多