IOS 7 Xcode搜索栏控制器实现

时间:2014-08-16 19:39:42

标签: xcode ios7 uisearchbar

Sunny感谢您的回复。我做了以下更改,现在通过单击tablecell或搜索结果,我被推送到DetailViewController但是,对于我点击的任何单元格,我得到相同的DetailView。我需要详细视图来显示不同的信息,具体取决于我点击的单元格。

我补充说:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"ShowDetails" sender: self];
    }
}

新segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
        DetailViewController *detailviewcontroller = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

        int row = [myIndexPath row];
        detailviewcontroller.DetailModal = @[_Title[row],_Attribute[row]];
    }
}

更新

我在实现代码时遇到困难的原因是因为要传递给detailview的信息在DetailViewController.h中,但是作为数组而不是字符串:

@property (nonatomic,strong) NSArray *Title;
@property (nonatomic,strong) NSArray *Attribute;
在viewdidload中我从plist中得到这些:

 NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    _Title = [dict objectForKey:@"caseName"];
    _Attribute= [dict objectForKey:@"attribute"];

使用以下内容填充表视图:

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

    if (cell == nil)
    {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
    }
    else
    {
        cell.TitleLabel.text = self.Title[indexPath.row];
        cell.AttributeLabel.text = self.Attribute[indexPath.row];

    }

    return cell;
}

然后在DetailViewController.h中:

@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;

@property (nonatomic,strong) IBOutlet UILabel *AttributeLabel;

@property (nonatomic,strong) NSArray *DetailModal;

DetailViewController.m:

_TitleLabel.text = _DetailModal[0];
_AttributeLabel.text = _DetailModal[1];

我的声音:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
        DetailViewController *detailviewcontroller = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

        int row = [myIndexPath row];
        detailviewcontroller.DetailModal = @[_Title[row],_Attribute[row]];
    }
}

我认为放入更多代码会有助于您获得更清晰的图像。在我的非专家意见中,我认为你的代码不适合我的原因是因为我的数据存储在一个数组(DetailModal)而不是字符串中。

感谢您抽出宝贵时间帮助我解决此问题

1 个答案:

答案 0 :(得分:0)

您应该实现TableView委托;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];    
    if (tableView == self.searchDisplayController.searchResultsTableView) {
            [self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
    }
    else {
            [self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
    }

}

更新: 您必须设置要在详细视图中显示的值。您可以在detailView的头文件中声明一个属性,您可以在prepareForSegue(或上面的didSelectRowAtIndexPath)中设置该属性。例如,在DetailViewController中,您可以这样做:

@interface DetailViewController: UIViewController

@property (weak, nonatomic) NSString *myDetailValue;

@end

然后在tableView:didSelectRowAtIndexPath:上面,在执行segue之前设置正确的值:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];    
        if (tableView == self.searchDisplayController.searchResultsTableView) {
// TODO select value from filteredList to set the detailViewController, e.g.:
self.detailedViewController.myDetailValue = valueFromFilteredListForIndexPath;
                [self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
        }
        else {
// TODO select value from regular list to set the detailViewController, e.g.:
self.detailedViewController.myDetailValue = valueFromRegularListForIndexPath;
                [self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
        }

    }

同样,你可以为segue做准备:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
        DetailViewController *detailviewcontroller = [segue destinationViewController];
        NSIndexPath *myIndexPath;

        if (self.inSearch) {
           myIndexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
           detailedViewController.myDetailValue = valueFromFilteredListForIndexPath;
        }
        else {
           myIndexPath = [self.tableView indexPathForSelectedRow];
           detailedViewController.myDetailValue = valueFromRegularListForIndexPath;
        }
    }
}