我设法以编程方式添加了tableview单元格,并且我还添加了一个带有作品的搜索功能!
我希望我制作的细胞像按钮一样,你知道当细胞右侧有一个箭头时,可以带你到另一个视图。
所以我的问题是:如何使单元格可单击并以编程方式将它们连接到我的下一个视图(所有视图都相同)。手动我可以通过单击单元格并选择Style:Basic,Accessory:Disclosure Indicator,然后通过“push”将它连接到下一个视图控制器,以便我可以获得导航栏。
我的代码:
@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
{ static 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]; }