0

我有 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

4

2 回答 2

0

在 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;
    }
  }
于 2013-08-19T06:59:42.293 回答
0

首先,您应该使用另一个可变数组来代替使用 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];
于 2013-08-19T09:06:36.770 回答