我编写了一个代码,可以将数据输入到tableView中,并过滤用户键入的内容。我的问题是如何将每个tableView单元格推送到新的viewController或DetailView。
这是代码。
#import "SearchBar.h"
@interface SearchBar ()
@end
@implementation SearchBar
{
NSMutableArray *totalStrings;
NSMutableArray *filteredStrings;
BOOL isFiltered;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
{
self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
totalStrings = [[NSMutableArray alloc]initWithObjects:
@"Apple - AAPL - Buy", @"Zacks Rank: Hold AAPL : Apple",
@"The Street Rank: Buy AAPL : Apple",
@"MarketWatch Rank: Hold AAPL : Apple",
@"InvestorPlace Rank: Hold AAPL : Apple",
@"Nasdaq Rank: Buy AAPL : Apple",
@"Target Price: $135 AAPL : Apple",
@"Our Pick - Strong Buy AAPL : Apple",
@"Google - GOOG - Buy", @"Zacks Rank: Hold GOOG : Google",
@"The Street Rank: Hold GOOG : Google",
@"MarketWatch Rank: Buy GOOG : Google",
@"InvestorPlace Rank: Buy GOOG : Google",
@"Nasdaq Rank: Buy GOOG : Google",
@"Target Price: $880 GOOG : Google",
@"Our Pick - Strong Buy GOOG : Google",
@"Amazon - AMZN - Buy", @"Zacks Rank: Hold AMZN : Amazon",
@"The Street Rank: Hold AMZN : Amazon",
@"MarketWatch Rank: Buy AMZN : Amazon",
@"InvestorPlace Rank: Buy AMZN : Amazon",
@"Nasdaq Rank: Buy AMZN : Amazon",
@"Target Price: $735 AMZN : Amazon",
@"Our Pick - Buy AMZN : Amazon",
@"Facebook - FB - Buy", @"Zacks Rank: Buy FB : Facebook",
@"The Street Rank: Hold FB : Facebook",
@"MarketWatch Rank: Buy FB : Facebook",
@"InvestorPlace Rank: Buy FB : Facebook",
@"Nasdaq Rank: Buy FB : Facebook",
@"Target Price: $130 FB : Facebook",
@"Our Pick - Strong Buy FB : Facebook",
@"Johnson & Johnson - JNJ - Hold", @"Zacks Rank: Hold JNJ : Johnson",
@"The Street Rank: Buy JNJ : Johnson",
@"MarketWatch Rank: Hold JNJ : Johnson",
@"InvestorPlace Rank: Buy JNJ : Johnson",
@"Nasdaq Rank: Buy JNJ : Johnson",
@"Target Price: $111 JNJ : Johnson",
@"Our Pick - Buy JNJ : Johnson",
@"Exxon Mobil - XOM - Hold", @"Zacks Rank: Hold XOM : Exxon",
@"The Street Rank: Hold XOM : Exxon",
@"MarketWatch Rank: Hold XOM : Exxon",
@"InvestorPlace Rank: Hold XOM : Exxon",
@"Nasdaq Rank: Hold XOM : Exxon",
@"Target Price: $84 XOM : Exxon",
@"Our Pick - Hold XOM : Exxon",
@"Netflix - NFLX - Hold", @"Zacks Rank: Hold NFLX : Netflix",
@"The Street Rank: Hold NFLX : Netflix",
@"MarketWatch Rank: Hold NFLX : Netflix",
@"InvestorPlace Rank: Buy NFLX : Netflix",
@"Nasdaq Rank: Buy NFLX : Netflix",
@"Target Price: $125 NFLX : Netflix",
@"Our Pick - Hold NFLX : Netflix",
@"Twitter - TWTR - Hold", @"Zacks Rank: Hold TWTR : Twitter",
@"The Street Rank: Sell TWTR : Twitter",
@"MarketWatch Rank: Hold TWTR : Twitter",
@"InvestorPlace Rank: Sell TWTR : Twitter",
@"Nasdaq Rank: Hold TWTR : Twitter",
@"Target Price: $29 TWTR : Twitter",
@"Our Pick - Sell TWTR : Twitter",nil];
}
}
- (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];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.myTableView resignFirstResponder];
}
- (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
{
cell.textLabel.text = [filteredStrings objectAtIndex:indexPath.row];
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)
据我所知,您希望显示有关在新UIViewcontroller上选择的UItableviewcell的详细信息。如果是这样,那么你需要使用
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"yourSegueNameHere" sender:self];
}
然后
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"yourSegueNameHere"]){
//pass the data to the ViewController
}
}
希望这有帮助。