gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:20:04 25 4
gpt4 key购买 nike

我正在尝试从动态 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com