2012-10-24 24 views
0

我正在開發一個應用程序,並且當我搜索其中一個單元時,例如,我在UItableview代碼中遇到問題:我想進入單元格60,然後在搜索欄中搜索它給我cell60它給我的是在表中的第一個單元格如何解決呢UISearchbar和UITableViewCell

AllItems是NSArray的和displayItems是NSMutableArray的

這個代碼

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



{ 


    //Crete an NSSring object that we can use as the eruse identifir 
static NSString *CellIdentifier = @"Cell"; 



//Check to see if wer can reuse a cell from a row that has just roolerd off the screen 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 



if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 

} 


cell.textLabel.text = [displayItems objectAtIndex:indexPath.row]; 

//if there are no cells to be reused creater new cell 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

//set the text attribute to whatever we are currently looking at in our array 


//Return the cell 
return cell; 
} 

這個代碼對於搜索欄

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 
if ([searchText length] == 0) { 
    [displayItems removeAllObjects]; 
    [displayItems addObjectsFromArray:allItems]; 


} else { 

    //here 

    [displayItems removeAllObjects]; 
    for (NSString * string in allItems){ 
     NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (r.location != NSNotFound){ 
      [displayItems addObject:string]; 
     } 
    } 
} 
[tableView reloadData]; 


} 

普萊舍幫助我

回答

0

你應該做的:

在標題:

@property(strong, nonatomic) NSMutableArray* displayItems; 
@property(strong, nonatomic) NSArray* allItems; 

在實施:

- (void) viewDidLoad { 
    ... 
    self.addItems = [NSArray arrayWithObjects:@"1", @"2", @"3"]; 
    self.displayItems = [NSMutableArray array]; 
} 

你的方法:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 

    if ([searchText length] == 0) { 
     [self.displayItems removeAllObjects]; 
     [self.displayItems addObjectsFromArray:allItems]; 

    } else { 

     [self.displayItems removeAllObjects]; 
     for (NSString * string in self.allItems){ 
     NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (r.location != NSNotFound){ 
      [self.displayItems addObject:string]; 
     } 
    } 

    [tableView reloadData]; 

} 

表視圖的數據源:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.displayItems count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 


    cell.textLabel.text = [self.displayItems objectAtIndex:indexPath.row]; 

    return cell; 

} 
+0

我編輯的主題,並加入seacrhbar – ffds

+0

好了,你要過濾你的細胞,你做的一切權利,然後,只要確保你的'displayItems'被保留,最好做一個'strong'屬性,並像'self.displayItems'一樣使用它,同時確保你返回'numberOfRowsInSection'中的正確數目的項目,它應該是'[self.displayItems count];'。 – Ezeki

+0

,不要忘記在某處分配'displayItems':self.displayItems = [NSMutableArray array]; – Ezeki