如何将基于故事板的 Header 和 CustomTableCell 添加到“搜索栏和搜索显示控制器" [英] How do I add storyboard-based Header and CustomTableCell to a “Search Bar and Search Display Controller”

查看:24
本文介绍了如何将基于故事板的 Header 和 CustomTableCell 添加到“搜索栏和搜索显示控制器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

演示文稿

我的是一个简单的项目:它由一个 NavigationController、ViewController 和一个搜索栏和搜索显示控制器"组成

Mine is a simple project: It consists of a NavigationController, ViewController, and a "Search Bar and Search Display Controller"

我的 .h 文件是

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>

@end

而我的 .m 文件是

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *data;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
    self.data=[[NSMutableArray alloc]init];
}

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


#pragma mark - data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Configure the cell.
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d: %@", indexPath.row, [_data objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark - delegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(mockSearch:) userInfo:searchString repeats:NO];
    return NO;
}

- (void)mockSearch:(NSTimer*)timer
{
    [_data removeAllObjects];
    int count = 1 + random() % 20;
    for (int i = 0; i < count; i++) {
        [_data addObject:timer.userInfo];
    }
    [self.searchDisplayController.searchResultsTableView reloadData];
}

@end

这就是整个程序.它有什么作用?用户进行搜索,数据显示在 TableView 中(类似于 google 搜索).

And that’s the entire program. What does it do? User makes a search and the data is displayed in a TableView (similar to a google search).

问题

  • 我需要为我的表格使用 CustomTableViewCell.我需要从情节提要构建 TableViewCell(易于可视化).我被故事板部分困住了.如何在没有 TableView 的情况下将 TableViewCell 放置在情节提要上?我有一个想法,我尝试过,但没有成功.这就是我所做的.我在情节提要中放置了一个永远不会被使用"的 TableViewController,其唯一目的是保存我的 CustomTableViewCell.然后在代码中,我将 TableViewCell 子类化并使用 IBOutlet 将情节提要 TableViewCell 的子视图链接到我的 CustomTableViewCell .然后我将我的单元格用作 CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];.这没有用.tableView 保持空白.而我对失败的猜测是 CustomTableViewCell 不属于正在出队的 tableView.

  • I need to use a CustomTableViewCell for my table. And I need to build the TableViewCell from the storyboard (easy to visualize). I am stuck with the storyboard part. How do I place a TableViewCell on the storyboard without a TableView to place it in? I had an idea, I tried it, but it didn’t work. Here is what I did. I placed a "never-to-be-used" TableViewController in the storyboard whose sole purpose is to hold my CustomTableViewCell. Then in code I subclass TableViewCell and use IBOutlet to link the sub-views of the storyboard TableViewCell to my CustomTableViewCell . And then I used my cell as CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];. This didn’t work. The tableView remained blank. And my guess for the failure is that CustomTableViewCell does not belong to the tableView being dequeued from.

我需要 UISearchBar 始终留在 NavigationBar 内.但到目前为止 self.searchDisplayController.displaysSearchBarInNavigationBar = YES; 并没有这样做.当我第一次启动应用程序时,搜索栏位于导航栏内.但只要我点击它,它就会在我的场景/屏幕中间撞到自己,永远不会离开.

I need the UISearchBar to always stay inside the NavigationBar. But so far self.searchDisplayController.displaysSearchBarInNavigationBar = YES; is not doing it. When I first start the app the searchBar is inside the NavigationBar. But as soon as I click on it, it smacks itself right in the middle of my scene/screen and never leaves.

我需要在我的 TableView 中添加一个 Header.我想用与 CustomTableViewCell 相同的方式,但使用 UIView 父类.但同样,CustomTableViewCell 部分失败了.

I need to add a Header to my TableView. Which I thought of doing the same way as the CustomTableViewCell, but with a UIView parent class. But again, the CustomTableViewCell portion failed.

恳求

感谢您为我提供的任何帮助.

Thank you for any help you can provide me.

更新

我想做的只是让我的用户启动服务器端搜索并在 tableView 中查看结果.这是一个基本的事情,我想这里的很多人都做过很多次.作为iOS新手,我被困住了.但是此时,我已经发布了我的整个项目(任何人都可以重建它),并且我已经详细解释了我试图解决问题的所有方法.因此,如果有人有一个他们不介意分享的示例项目,那将非常有帮助.或者如果你知道一个 git 项目,请给它一个链接.谢谢.

All I am trying to do is allow my users to launch a server side search and view the results in a tableView. This is such a basic thing, I image many people here have done this a number of times. Being new to iOS, I am stuck. But at this point, I have posted my entire project (anyone can reconstruct it), and I have explained in details all the ways I tried to solve the problem. So if someone has a sample project they don’t mind sharing, it would help very much. Or if you know of a git project please put a link to it. Thanks.

推荐答案

如果你想在 IB 中创建一个独立的视图(不在视图控制器中),那么你应该在 xib 文件中进行,而不是在故事板中进行.您可以在应用程序中拥有任意数量的故事板和 xib 文件;它们可以自由混合.要在 xib 文件中创建新单元格,只需转到新建文件 --> 用户界面 --> 空,然后拖入 UITableViewCell.添加您想要的任何子视图,然后在您的表格视图控制器(或您的表格视图数据源的任何类)中,使用 registerNib:forIdentifier 注册 xib 文件:(通常,您在 viewDidLoad 中执行此操作).

If you want to make a stand-alone view (not in a view controller) in IB, then you should do it in a xib file, not a storyboard. You can have as many storyboard and xib files in an app as you want; they can be mixed freely. To make a new cell in a xib file, just go to New File --> User Interface --> Empty then drag in a UITableViewCell. Add any subviews you want, and in your table view controller (or whatever class is your table view data source), register the xib file with, registerNib:forIdentifier: (usually, you do this in viewDidLoad).

这篇关于如何将基于故事板的 Header 和 CustomTableCell 添加到“搜索栏和搜索显示控制器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆