请帮忙!! tablecell适用于没有搜索栏的推送到detailview但是当我在搜索栏上键入一个单词时,它显示正确,但它没有推送到detailview。 感谢您的帮助...
·H
@interface xtzViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
{
IBOutlet UITableView *mytableview;
IBOutlet UISearchBar *SearchBar;
NSMutableArray *titleArray;
NSMutableArray *subtitleArray;
NSMutableArray *filteredString;
BOOL isFilltered;
}
@end
的.m
- (void)viewDidLoad
{
[super viewDidLoad];
mytableview.delegate = self;
mytableview.dataSource = self;
SearchBar.delegate = self;
titleArray = [[NSMutableArray alloc] initWithObjects:@"goat ",@"cow ",@"tiger",@"lion",@"elephant",nil];
subtitleArray =[[NSMutableArray alloc] initWithObjects:@"wX<w (rHR< cg)",@"o;vDRuwkR(vXw>csK;cHed;o;wkRtd.tCd)<*kRuhRto; (vXw>urXur.tylR)<o;vDRxl.",@"vhjbBGHvg",@"uNdD<",@"usdawYHgD",nil];
filteredString = [[NSMutableArray alloc] init];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[mytableview resignFirstResponder];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
isFilltered = NO;
}else
{
isFilltered = YES;
filteredString = [[NSMutableArray alloc]init];
for(NSString *str in titleArray)
{
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[filteredString addObject:str];
}
}
}
[mytableview reloadData];
}
-(NSInteger) numberOfSectionInTableView:(UITableView *)tableview
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFilltered)
{
return [filteredString count];
}
else
{
return [titleArray count];
return [subtitleArray count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(!isFilltered)
{
cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
}else
{
cell.textLabel.text = [filteredString objectAtIndex:indexPath.row];
}
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"detailSegue"])
{
NSIndexPath *indexpath = nil;
NSString *titlestring = nil;
NSString *subtitlestring =nil;
indexpath = [mytableview indexPathForSelectedRow];
titlestring = [titleArray objectAtIndex:indexpath.row];
subtitlestring = [subtitleArray objectAtIndex: indexpath.row];
[[segue destinationViewController] setTitlecontents:titlestring];
[[segue destinationViewController] setSubtitlecontents:subtitlestring];
}
}
@end