从动态表格单元格链接到特定视图控制器

时间:2013-01-29 18:06:37

标签: ios uiviewcontroller uitableview uisearchbar

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

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

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

SearchViewController.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视图控制器?

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

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要实现tableView:didSelectRowAtIndexPath:在您选择行时调用它。您可以使用传入该方法的indexPath通过查询数据源来获取该行的数据。然后,您可以使用您需要的任何逻辑来选择下一个视图控制器。你可以通过调用performSegueWithIdentifier来实现。