我设法以编程方式添加了 tableview 单元格,我还添加了一个搜索功能!
我希望我制作的单元格像按钮,你知道当单元格右侧有一个箭头时可以带你到另一个视图。
所以我的问题是:如何使单元格可点击并以编程方式将它们连接到我的下一个视图(全部连接到同一个视图)。手动我可以通过单击单元格并选择样式:基本,附件:披露指示器,然后使用“推”将其连接到下一个视图控制器,以便我可以获得导航栏。
我的代码:
@interface Avfall () { NSMutableArray *totalStrings; NSMutableArray *filteredStrings; BOOL isFiltered; } @end @implementation Avfall - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.mySearchBar.delegate=self; self.myTableView.delegate=self; self.myTableView.dataSource=self; totalStrings=[[NSMutableArray alloc]initWithObjects:@"One", @"Two",@"Three", @"Four",nil]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if(searchText.length==0) { isFiltered=NO; } else { isFiltered=YES; filteredStrings=[[NSMutableArray alloc]init]; for (NSString *str in totalStrings) { NSRange stringRange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch]; if(stringRange.location != NSNotFound) { [filteredStrings addObject:str]; } } } [self.myTableView reloadData]; } //table view datasource and delegate methods... -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(isFiltered) { return [filteredStrings count]; } return[totalStrings count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 静态 NSString *Cellidentifier=@"Cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if(!cell) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier]; } if(!isFiltered) { cell.textLabel.text=[totalStrings objectAtIndex:indexPath.row]; } else //it is filtered { cell.textLabel.text = [filteredStrings objectAtIndex:indexPath.row]; } return cell; } -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [self.myTableView resignFirstResponder]; }