【问题标题】:How to use the switch command with a search bar如何使用带有搜索栏的 switch 命令
【发布时间】:2013-02-06 16:15:37
【问题描述】:

我有一个使用以下数据编程的表格视图。它还具有编程到页面上的工作搜索功能。我正在尝试使用“开关”系统根据按下的单元格转移到特定的视图控制器。

- (void)viewDidLoad
{
[super viewDidLoad];

smtArray = [[NSArray alloc] initWithObjects:
                    [Smt SSSOfCategory:@"P" name:@"HW"],
                    [Smt SSSOfCategory:@"P" name:@"WI"],
                    [Smt SSSOfCategory:@"P" name:@"WC"],
                    [Smt SSSOfCategory:@"P" name:@"C"],nil];

//搜索功能代码

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Smt *smt = nil;


if (tableView == self.searchDisplayController.searchResultsTableView) {
    smt = [filteredSmtsArry objectAtIndex:indexPath.row];


    switch (indexPath.row) {
        case 0:

            [self performSegueWithIdentifier:@"One" sender:self];
            break;

        case 1:
            [self performSegueWithIdentifier:@"Two" sender:self];
            break;

        default:
            break;
    }
}

}

目前,我对如何根据 tableview 单元格中的对象执行 segue 感到困惑。显然,目前 segues 依赖于 indexPath.row,这对于可搜索的表是无用的。 (我知道 segues 目前仅在过滤数据时才起作用)。

任何建议将不胜感激!

干杯

【问题讨论】:

    标签: ios uitableview xcode4.5 uisearchbar


    【解决方案1】:

    你可以使用

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
            NSString *content = selectedCell.textLabel.text;
    
            if ([content isEqualToString:"One"]) [self performSegueWithIdentifier:@"One" sender:self];
            else if ([content isEqualToString:"Two"]) [self performSegueWithIdentifier:@"Two" sender:self];        
        }
    }
    

    在你的方法中。然后你就可以从中得到你想要的任何东西。

    【讨论】:

    • 嗨,Iblue,感谢您的回复。抱歉,如果这是一个新手请求,但您能否更具体地回答您的问题?您在方法中的何处实现该代码,具体而言,您如何根据单元格中的对象使用它来激活 segue?我还是很困惑...感谢您的宝贵时间,
    • @user2019029 这能回答你的问题吗?
    • Iblue,你是个天才。我无法告诉你这让我发疯了多久。非常感谢。一旦我达到 15 声望,我就会投票赞成你的答案。再次感谢。
    【解决方案2】:

    我想知道你为什么使用 "One" 、 "Two" ... 作为标识符,为什么不使用 smtArrays 对象名称作为标识符?如果您担心标识符过时,可以使用 SSSOfCategory + name 。而且你的代码会更完美更易读

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        Smt *smt = nil;
    
    
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            smt = [filteredSmtsArry objectAtIndex:indexPath.row];
    
            [self performSegueWithIdentifier:smt.name sender:self];
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-04
      • 2019-02-20
      • 2020-07-07
      • 2018-01-23
      • 2022-12-02
      • 1970-01-01
      • 2018-11-22
      • 2012-08-02
      相关资源
      最近更新 更多