从动态表格单元链接到特定的视图控制器
Posted
技术标签:
【中文标题】从动态表格单元链接到特定的视图控制器【英文标题】:linking from dynamic table cells to specific view controllers 【发布时间】:2013-01-29 18:06:37 【问题描述】:我正在尝试从动态表视图单元(作为搜索结果表的一部分)链接到特定视图控制器
目前我实现的代码是:
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】:你需要实现 tableView:didSelectRowAtIndexPath: 当你选择一行时调用。您可以通过使用传递给该方法的 indexPath 查询数据源来获取该行的数据。然后,您可以使用所需的任何逻辑来选择接下来要转到哪个视图控制器。你可以通过调用 performSegueWithIdentifier 来做到这一点。
【讨论】:
感谢 rdelmar 的回复!只是为了澄清一下,“使用传递给该方法的 indexPath 查询数据源”究竟是什么意思?抱歉,如果这是一个愚蠢的问题!再次感谢您的宝贵时间 使用与您在cellForRowAtIndexPath
中编写的非常相似的逻辑,您可以使用选定的indexPath.row
从tableView:didSelectRowAtIndexPath:
函数中确定选定的行代表什么sysT
对象。
感谢 oltman 和 rdelmar 的回复!除了如何使用 indexPath.row 来确定所选行代表什么 sysT 对象之外,我了解所有内容...您能否为编码初学者进一步澄清这一点?再次感谢!
@user2019724,您在 cellForRowAtIndexPath 方法中使用了这样的一行: sysT = [sysTArray objectAtIndex:indexPath.row] - 这就是您从对象数组中获得所需的特定 sysT 对象的方式.您在 didSelectRowAtIndexPath 中执行完全相同的操作(或使用过滤后的sysTArry 数组,我不确定您想要哪个)。以上是关于从动态表格单元链接到特定的视图控制器的主要内容,如果未能解决你的问题,请参考以下文章