2010-11-08 2 views
1

Я хочу добавить простую панель поиска в виде таблицы. это мой код: -Нажмите на поисковый бар, чтобы завершить мою программу на Iphones, используя Objective C

@interface RootViewController: UITableViewController { NSArray * list;

IBOutlet UISearchBar *searchBar; 


NSMutableArray *searchresult; 

BOOL isSearchon; 

BOOL canSelectRow; 

} @property (неатомической, сохранить) NSArray * список; @property (nonatomic, сохранить) IBOutlet UISearchBar * searchBar; - (void) doneSearching: (id) отправитель;

- (void) searchlist;

@end

// в ом

@implementation RootViewController

список @synthesize, SearchBar;

- (недействительными) doneSearching: (ID) отправитель

{ isSearchon = NO;

canSelectRow=YES; 

self.tableView.scrollEnabled=YES; 

self.navigationItem.rightBarButtonItem=nil; 

[searchBar resignFirstResponder]; 

[self.tableView reloadData]; 

}

- (пустоты) спискупоиска

{NSString * SearchText = searchBar.text;

[searchresult removeAllObjects]; 



for(NSString *str in list) 
{ 
    NSRange titleResultsRange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 


    if (titleResultsRange.length > 0) 
     [searchresult addObject:str]; 
} 

}

  • (пустоты) searchBarSearchButtonClicked: (UISearchBar *) theSearchBar {

    [самостоятельного спискупоиска]; }

  • (пустоты) SearchBar: (UISearchBar *) theSearchBar textDidChange: (NSString *) SearchText {

, если ([длина SearchText]> 0)

{isSearchon = ДА;

canSelectRow=YES; 

self.tableView.scrollEnabled=YES; 

[self searchlist];} 

еще { isSearchon = NO;

canSelectRow=NO; 

self.tableView.scrollEnabled=NO; 

} 

[self.tableView reloadData]; 

}

  • (пустоты) viewDidLoad { [супер viewDidLoad];

    list = [[NSArray alloc] initWithObjects: @ "rohan", @ "vidhya", @ "kavita", @ "pushkar", nil];

    self.tableView.tableHeaderView = searchBar;

    searchBar.autocorrectionType = UITextAutocorrectionTypeYes;

    searchresult = [[NSMutableArray alloc] init];

    isSearchon = NO;

    canSelectRow = YES;

    self.navigationItem.title = @ "Имена";

}

- (недействительными) searchBarTextDidBeginEditing: (UISearchBar *) SearchBar

{ isSearchon = ДА;

canSelectRow=NO; 

self.tableView.scrollEnabled=NO; 

self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneSearching:)]autorelease]; 

}

  • (NSInteger) numberOfSectionsInTableView: (UITableView *) Tableview {

    возвращение 1; }

// Настройка количества строк в виде таблицы. - (NSInteger) Tableview: (UITableView *) Tableview numberOfRowsInSection: (NSInteger) раздел { , если (isSearchon) { возврата [число SearchResult];}

else{ 

     return [list count];} 

}

// Настроить появление ячеек таблицы. - (UITableViewCell *) Tableview: (UITableView *) Tableview cellForRowAtIndexPath: (NSIndexPath *) indexPath {

static NSString *CellIdentifier = @"Cell"; 


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

if(isSearchon) 
{ 
    cell.text=[searchresult objectAtIndex:indexPath.row];} 

    else{ 
     cell.text=[list objectAtIndex:indexPath.row];} 

// Configure the cell. 

return cell; 

}

  • (пустоты) dealloc { [супер dealloc]; [поискBar release]; }

@end

ответ

1

Я отредактировал мой код в моем вопросе ... это работает отлично ЯО .... кто готов использовать SearchBar в Tableview в Iphones может передать код.