【问题标题】:UISearchbar with UIStoryboard: segues and custom cells do not work correctly带有 UIStoryboard 的 UISearchbar:segues 和自定义单元格无法正常工作
【发布时间】:2012-08-25 11:31:17
【问题描述】:

我正在使用情节提要并想为我的 UITableView 实现一个 UISearchbar。 UISearchbarController 生成一个新的 UITableView,我使用以下策略:

if (tableView == self.tableView)
   //Populate table view with normal data
else
   //Populate table view with search data

第一个问题是处理自定义单元格。我必须在 cellForRowAtIndexPath 中实例化它们。通常,您会从 nib 文件中执行此操作。我如何从情节提要中做到这一点? dequeueReusableCellWithIdentifier 返回 nil。

第二个问题涉及从表视图到详细视图的转换。 segue 在普通表视图中启动,而不是在搜索表视图中。由于 Storyboards 隐藏了所有内容,我不知道如何将 segues 分配给搜索表视图的单元格。

有人可以帮忙吗?谢谢!

【问题讨论】:

    标签: ios uisearchbar uistoryboard


    【解决方案1】:

    关于第一个问题,我通过将 ViewController 类型从 UITableViewController 更改为 UIViewController 来解决它,然后在其中添加表视图并为表视图添加 IBOutlet

    @property (strong, nonatomic) IBOutlet UITableView *_tableView;
    

    使用插座将表格单元格出列

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MyTableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:productMasterCellIdentifier];
        // .. modify your cell
        return cell;
    }
    

    【讨论】:

    • 这似乎运作良好;我保留了UITableViewController,只是使用了:self.tableView dequeueReusableCellWithIdentifier:...
    • 如果搜索和主表视图都指向同一个目标视图控制器,则使用 @penfold 建议的 [self.tableView dequeueReusableCellWithIdentifier...] 效果很好。但是如果目标视图控制器不同(即使这不是常见的情况),那么你就不能这样做,因为搜索栏无法正常工作。我用我的身体测试过……
    【解决方案2】:

    关于第二个问题:不要从单元格链接segue,而是从视图控制器链接。您需要在情节提要中使用不同的名称制作两个 segue。然后在选择行时手动调用 segue:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
            [self performSegueWithIdentifier:@"fromSearchResults" sender:self];
        }
        else {
            [self performSegueWithIdentifier:@"fromAll" sender:self];
        }
    }  
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"fromSearchResults"]) {
            ..........
            ..........
        }
        else {
            ..........
            ..........
        }
    }
    

    【讨论】:

      【解决方案3】:

      这就是我所做的:

      1) 我在 initWithStyle:reuseIdentifier: 中初始化了自定义单元格,然后在 layoutSubviews 中实现了布局。所以我仍然不知道如何从情节提要中加载单元格。

      2) 我将视图推送到导航堆栈上,而不是使用情节提要转场。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-08
        • 1970-01-01
        相关资源
        最近更新 更多