uiSearchDisplayController 防止 tableview 改变背景颜色(背景保持灰色)

标签 ios uitableview ios7 searchdisplaycontroller

这可能是 iOS 7 中的一个错误: 我使用 Storyboard将 Search Bar 和 Search Display Controller 拖入我的 tableview Controller 中。之后更改了我的 tableview 的背景颜色。 我正在使用 AppCoda 的“how-to-add-search-bar-uitableview”中的示例来演示此问题。我只在 ViewDidLoad 中更改了带有 1 行附加组件的示例代码:

   [_tableView setBackgroundColor:[UIColor blackColor]];

在这里查看我的屏幕截图:

  1. tableview 的底部已经变为黑色: enter image description here

  2. 但尽管 tableview 的背景设置为黑色,但顶部仍保持灰色:enter image description here 只有当我拖入“搜索栏和搜索显示 Controller ”时才会发生这种情况。如果我从 Storyboard 中删除“搜索显示 Controller ”,一切都很好。

最佳答案

UITableViewController 中使用 UISearchDisplayController 时,请务必记住您正在处理两个 TableView 。

因此,当您将“搜索栏和搜索显示 Controller ”拖到 UITableView 中时(假设您是在 UIStoryboard 中执行此拖动操作),您实际上已添加另一个 UITableView,激活后必须由 UITableViewController 文件中的代码以与任何其他 UITableView 相同的方式进行管理。

考虑一下:

表示完整数据集的 UITableView 可以使用 self.tableView(或者如您所写,合成的 _tableView)调用。

可以使用 self.searchDisplayController.searchResultsTableView 调用代表过滤数据集(使用您的搜索条件过滤)的 UITableView

如果您希望您的默认 UITableView 和搜索 UITableView 背景颜色都显示黑色,我可以建议此代码(在我的应用程序的 TVC 中工作)...

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor blackColor]];

    [self.searchDisplayController.searchResultsTableView setBackgroundView:nil];
    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]];
}

...

更新

既然我已经完成了我的长篇大论,我同意 Xcode 中确实存在某种“错误”。如果您删除了原始的 UISearchBar,然后将一个新的添加到 UITableViewController,那么在连接它之前,执行构建并运行。黑色背景在表格 View 的下方和上方可见。只有在将 UISearchBar 设置为 UISearchDisplayController 的导出后,黑色背景才会“替换”为灰色背景。

所以解决方案...

感谢OlofDifferent background colors for the top and bottom of a UITableView 的回答.

替换我在上面用这段代码编写的代码,在你的viewDidLoad 方法的末尾:

- (void)viewDidLoad {
    [super viewDidLoad];

    ...<other code>...

    CGRect frame = self.tableView.bounds;
    frame.origin.y = -frame.size.height;
    UIView* viewBackgroundBlack = [[UIView alloc] initWithFrame:frame];
    [viewBackgroundBlack setBackgroundColor:[UIColor blackColor]];
    [self.tableView addSubview:viewBackgroundBlack];

    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor blackColor]];

    [self.searchDisplayController.searchResultsTableView setBackgroundView:nil];
    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]];
}

关于uiSearchDisplayController 防止 tableview 改变背景颜色(背景保持灰色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23026531/

相关文章:

objective-c - 将背景图像设置为uitableview?

ios - 在 iOS 7 的 UITableView 上指示加载事件的正确方法是什么?

iOS 7 AutoLayout "Top Space to Top Layout Guide"在 Interface Builder 中没有控制拖动?

iphone - NSDate 字符串未显示在 TableViewCell 中

ios - 有没有办法让子类不响应其父类(super class)实现的方法,并让编译器抛出错误?

ios - 如何以编程方式将 UIBarButtonItem 添加到我的导航 Controller ?

ios - 从数组中的数组访问对象(在模型类中定义)

iphone - 右对齐 EntryElement

ios - -[__NSDictionaryI rangeOfString :options:]: unrecognized selector sent to instance

ios - 自定义 UITableViewCell 按钮和 RowSelected (iOS/Monotouch)