нажатие на панель поиска завершает мою программу на Iphones, используя Objective C - PullRequest
1 голос
/ 08 ноября 2010

я хочу добавить простую панель поиска в табличном представлении .. программа запускается без ошибок .. но когда я пытаюсь добавить текст в панель поиска, программа завершается ... следующий код: -

@ interface RootViewController: UITableViewController { NSArray * list;

IBOutlet UISearchBar *searchBar;


NSMutableArray *searchresult;

BOOL isSearchon;

BOOL canSelectRow;

} @property (nonatomic, retain) NSArray * list; @property (nonatomic, retain) IBOutlet UISearchBar * searchBar; - (Недействительными) doneSearching: (идентификатор) отправителя;

- (Недействительными) спискупоиска;

@ конец

// дюйм.

@ реализация RootViewController

@ синтезировать список, панель поиска;

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

{ isSearchon = НЕТ;

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];
}

}

  • (void) searchBarSearchButtonClicked: (UISearchBar *) theSearchBar {

    [собственный список поиска]; }

  • (void) searchBar: (UISearchBar *) theSearchBar textDidChange: (NSString *) searchText {

if ([searchText length]> 0)

{isSearchon = YES;

canSelectRow=YES;

self.tableView.scrollEnabled=YES;

[self searchlist];}

еще { isSearchon = NO;

canSelectRow=NO;

self.tableView.scrollEnabled=NO;

}

[self.tableView reloadData];

}

  • (void) viewDidLoad { [super viewDidLoad];

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

    self.tableView.tableHeaderView = searchBar;

    searchBar.autocorrectionType = UITextAutocorrectionTypeYes;

    searchresult = [[NSMutableArray alloc] init];

    isSearchon = NO;

    canSelectRow = ДА; * 1 068 *

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

}

- (void) searchBarTextDidBeginEditing: (UISearchBar *) searchBar

{ isSearchon = YES;

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) { return [количество результатов поиска];}

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;

}

  • (void) dealloc { [супер сделка]; [релиз searchBar]; }

@ конец

1 Ответ

1 голос
/ 10 ноября 2010

Я отредактировал свой код в своем вопросе ... он работает нормально, теперь .... AnyOne, желающий использовать панель поиска в табличном представлении в Iphones, может ссылаться на код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...