【问题标题】:Link a search bar cell label to view controller将搜索栏单元格标签链接到视图控制器
【发布时间】:2014-01-15 21:15:28
【问题描述】:

我正在尝试在 Xcode 中创建一个搜索栏,因此当您搜索一个项目时,您可以单击搜索结果单元格,它将转到视图控制器,但现在它所做的只是转到海景控制器并仅显示我单击的单元格的标签。

这是我的 .m 文件

@interface ListTableViewController () <UISearchDisplayDelegate>

@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSArray *searchResults;

@end

@implementation ListTableViewController

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

- (void)viewDidLoad
{
[super viewDidLoad];

self.dataArray = [NSArray arrayWithObjects:@"Tomb Raider", @"Little Big Planet",     @"Unchanted 3", @"BlackOps 2", nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)filterContentForSearchText: (NSString *) searchText
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
self.searchResults = [self.dataArray filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString];
return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:   (NSInteger)section
{
if (tableView == self.tableView) {
    return [self.dataArray count];
} else { // (tableView == self.searchDisplayController.searchResultsTableView)
    return  [self.searchResults count];
}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (tableView == self.tableView) {
    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}



#pragma mark - Table view delegate

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetails"]) {
    DetailsViewController *dvc = segue.destinationViewController;

    NSIndexPath *indexPath = nil;

    if ([self.searchDisplayController isActive]) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        dvc.sendLabel = [self.searchResults objectAtIndex:indexPath.row];
        return;
    } else{
        indexPath = [self.tableView indexPathForSelectedRow];
        dvc.sendLabel = [self.dataArray objectAtIndex:indexPath.row];
        return;
    }
}
}


@end

谢谢,一切都会有帮助的。

【问题讨论】:

  • 我不想代替你写所有控制器代码,但我认为如果你阅读本教程appcoda.com/how-to-add-search-bar-uitableview你应该找到答案。
  • 我已经尝试过那个教程,它也有同样的问题。
  • 你不能推送新的视图控制器,还是什么?说真的,我不明白你。请说明您的问题
  • 当用户搜索一个词时,我希望他们点击该词并将他们定向到一个视图控制器,该控制器将为每个搜索词显示不同的信息。此外,不仅仅是一个标签。

标签: xcode search view controller


【解决方案1】:

你必须实现这个委托方法:

// Method returns the clicked row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   YourPushedViewController *ypVC = [[YourPushedViewController alloc] initWithNibName:@"YourPushedViewController" bundle:nil];
  ypVC.selectedText = myResultArray[indexPath.row]; 


   [self.navigationController pushViewController:ypVC animated:YES]; 

}

该方法是选择行并将选择的结果传递给另一个视图的响应。

这是给你的提示:Passing Data between View Controllers

【讨论】:

  • 我试过这个,它一直说“使用未声明的标识符'myresultarray'”我遇到问题的部分如下 - ypVC.ypvcText = myResultArray[indexPath.row];
  • 当然,因为 myResultArray 是我自己的变量,所以您必须通过自定义数组交换此变量。而 YourPushedViewController 是我的 ViewController,您必须使用公共属性 selectedText 将自定义 ViewController 添加到项目中
  • 好的,但是我会将 myResultArray 更改为什么。可以举个例子吗?
  • 这里你有一个关于搜索栏的很好的教程:appcoda.com/how-to-add-search-bar-uitableview 如果你对这个任务仍然有问题,我会为你创建一个示例代码。不过先试着自己解决这个问题,过几天我就有空去做了。这个教程很不错。祝你好运
  • 试过教程,没用。如果您能在空闲时间向我提供一些示例代码,将不胜感激,谢谢。
猜你喜欢
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
相关资源
最近更新 更多