2016-02-27 38 views
0

我寫了一個代碼,它將數據輸入到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

從我個人理解,你想顯示有關新的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 
} 
} 

希望這有助於。