我做了一个tableview,我在过滤单元格上有一个搜索栏。我想知道如何让每个单独的单元格进入详细视图并显示图像。
这是代码。
#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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我很想听听有关如何从每个结果中添加segue的回复。
答案 0 :(得分:0)
在didSelectRowAtIndexPathMethod
中使用performSegueWithIdentifier
手动调用您在故事板中创建的segue。
答案 1 :(得分:0)
我建议你将来自UITableviewController的segue添加到下一个viewController,正如@blake Lockley建议的那样,在didSelectRowAtIndexPath上选择uiTableviewCell时执行segue