1

我正在尝试从动态表视图单元(作为搜索结果表的一部分)链接到特定的视图控制器

到目前为止我实现的代码是:

SearchViewController.h

import <UIKit/UIKit.h>

@interface SearchViewController : UITableViewController <UISearchDisplayDelegate, UISearchDisplayDelegate>
@property (strong,nonatomic) NSArray *sysTArray;
@property (strong,nonatomic) NSMutableArray *filteredsysTArry;
@property IBOutlet UISearchBar *sysTSearchBar;
@end

搜索视图控制器.M

#import "SearchViewController.h"
#import "sysT.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

@synthesize sysTArray;
@synthesize filteredsysTArry;
@synthesize sysTSearchBar;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

sysTArray = [NSArray arrayWithObjects:
                [sysT sysTOfCategory:@"p" name:@"H1"],
                [sysT sysTOfCategory:@"p" name:@"W2"],
                [sysT sysTOfCategory:@"p" name:@"W3"],
                [sysT sysTtOfCategory:@"p" name:@"C4"],
                [sysT sysTOfCategory:@"c" name:@"O5"],
                [sysT sysTOfCategory:@"c" name:@"C6"],
                [sysT sysTOfCategory:@"a" name:@"L7"], nil];

self.filteredSysTArry = [NSMutableArray arrayWithCapacity:[sysTArray count]];

[self.tableView reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredsysTArry count];
}else{
return [sysTArray count];
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}     

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:      (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];
}

SysT *sysT = nil;

if (tableView == self.searchDisplayController.searchResultsTableView) {
sysT = [filteredsysTArry objectAtIndex:indexPath.row];
}else{
sysT = [sysTArray objectAtIndex:indexPath.row];
}

cell.textLabel.text = sysT.name;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

}





#pragma mark Search Filtering

-(void)filterContentForSearchText:(NSString*) searchText scope:(NSString*)scope {
[self.filteredSysTArry removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",    searchText];
filteredSysTArry = [NSMutableArray arrayWithArray:[sysTArray   filteredArrayUsingPredicate:predicate]];

}

#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller     shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:
 [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}

-(BOOL) searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
 [[self.searchDisplayController.searchBar scopeButtonTitles]   objectAtIndex:searchOption]];
 return YES;}



@end

如何根据动态单元格内的数据启动特定的视图控制器?

更详细地说,如果用户搜索 H1,然后单击该动态单元格,我将如何显示相关的 H1 视图控制器?

正如您可能从我非常粗略的代码中看出的那样,我正处于陡峭的学习曲线上。如果你能把你的答案尽可能地作为婴儿证明,那就太棒了,真的会帮助我。(另外,我正在使用故事板)。

谢谢!

4

1 回答 1

1

您需要实现 tableView:didSelectRowAtIndexPath: 当您选择一行时调用它。您可以通过使用传递给该方法的 indexPath 查询数据源来获取该行的数据。然后,您可以使用所需的任何逻辑来选择接下来要转到哪个视图控制器。您可以通过调用 performSegueWithIdentifier 来做到这一点。

于 2013-01-29T18:13:25.403 回答