UISearchbar 与 UIStoryboard:segues 和自定义单元格无法正常工作

发布于 2025-01-04 16:16:16 字数 501 浏览 7 评论 0原文

我正在使用情节提要,并想为我的 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 在普通表视图中启动,但不在搜索表视图中启动。由于故事板隐藏了所有内容,我不知道如何将 Segues 分配给搜索表视图的单元格。

有人可以帮忙吗?谢谢!

I am using storyboards and want to implement a UISearchbar for my UITableView. The UISearchbarController generates a new UITableView and I use the following strategy:

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

The first problem is to deal with custom cells. I have to instantiate them in cellForRowAtIndexPath. Normally you would do that from a nib file. How do I do this from with storyboards? dequeueReusableCellWithIdentifier returns nil.

The second problem concerns the segue from the table view to the detail view. The segue is initiated in the normal table view, but not in the search table view. Since Storyboards hides everything I have no idea how to assign segues to cells of the search table view.

Could anyone help, please? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皓月长歌 2025-01-11 16:16:16

关于第一个问题,我通过将 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;
}

Regarding the first problem i solved it by changing the ViewController type from UITableViewController to UIViewController , then adding table view inside it and add IBOutlet for the table view

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

use the outlet to dequeue the table cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:productMasterCellIdentifier];
    // .. modify your cell
    return cell;
}
初与友歌 2025-01-11 16:16:16

关于第二个问题:不要从单元格链接segue,而是从视图控制器链接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 {
        ..........
        ..........
    }
}

About the second problem: don't link the segue from the cell, but from the viewcontroller. You will need to make two segues in your storyboard with different names. Then manually invoke the segue when the row is selected:

- (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 {
        ..........
        ..........
    }
}
一城柳絮吹成雪 2025-01-11 16:16:16

所以这就是我所做的:

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

2)我将视图推送到导航堆栈上,而不是使用故事板转场。

So here is what I did:

1) I initialized the custom cells in initWithStyle:reuseIdentifier:, then implemented the layout in layoutSubviews. So I still have no idea how to load the cells from the storyboards.

2) I pushed the view on the navigation stack instead of using the storyboard segues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文