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

查看:70
本文介绍了如何将基于故事板的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 file is

and my .m file is

#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中(类似于谷歌搜索)。

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。然后在代码I中继承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; 没有这样做。当我第一次启动应用程序时,searchBar位于NavigationBar中。但是当我点击它时,它就会在我的场景/屏幕中间自行打开并且永远不会离开。

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.

我需要添加一个Header到我的TableView。我想到的方法与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.

PLEA

感谢您提供给我的任何帮助。

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文件中执行,而不是在故事板中执行。您可以根据需要在应用程序中包含尽可能多的storyboard和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天全站免登陆