ios - 从动态表格单元格链接到特定的 View Controller

标签 ios uiviewcontroller uitableview uisearchbar

我正在尝试从动态 TableView 单元格(作为搜索结果表的一部分)链接到特定的 View Controller

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

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

如何根据动态单元格中的数据启动特定的 View Controller ?

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

正如您可能从我非常粗糙的代码中看出的那样,我正处于陡峭的学习曲线上。如果您能尽可能将您的答案作为婴儿证明,那就太棒了,并且真的会帮助我。 (此外,我正在使用 Storyboard)。

谢谢!

最佳答案

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

关于ios - 从动态表格单元格链接到特定的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14589357/

相关文章:

ios - 添加多个 Pod 使 iOS 应用程序启动时间增加 10 秒以上

ios - 将 NSFontAttributeName 设置为粗体

ios - 如何在 iOS 15 及更早版本上支持程序化 View 关闭

iOS:使表格 View 单元格(或 TextView ?)根据需要增大或缩小

iOS 多线程问题

ios - ViewController 不访问您的元素

iphone - 将选择器(重新加载)发送到当前 View

打开 UIViewController 的 UITextView 中的 iOS 链接

ios - 此类不符合键标签的键值编码

iPhone UITableView 嵌套部分