2013-08-19 25 views
0

我有ipad中有tableView顯示內容,我希望當用戶在搜索文本框中輸入任何數據時,匹配記錄應該顯示在tableView中。如何從tableView中使用textfield搜索ipad中的數據

- (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

回答

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; 
    } 
    } 
+0

ABCArray是陣列,其中它將搜索示出表中的數據平均排列 –

+0

是,ABCArray是其在numberofRowsInTableView方法,使用的陣列。 – Tendulkar

+0

爲什麼我們使用firstLetter爲空 –

0

首先改爲使用ap填充表格數據pdelegate數組,你應該使用另一個可變數組,它將在開始時擁有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]; 
相關問題