7

新しい UISearchController を使用する iOS 8 アプリを構築しています。検索コントローラーに関連するテーブル ビューでは、セクション インデックスを使用して、ユーザーがテーブルのあるセクションから次のセクションにすばやくジャンプできるようにしています。正常に動作していますが、セクション インデックスがテーブル/検索コントローラーの検索バーと重なっています。以前にこの問題に遭遇した人はいますか? もしそうなら、どのように解決しましたか? 以下は、検索コントローラーを初期化する方法です。

self.resultsTableController = [self.storyboard instantiateViewControllerWithIdentifier:[SelectSpecialtySearchResultsTVC storyboardId]];
UINavigationController *searchResultsNavController = [[UINavigationController alloc]initWithRootViewController:self.resultsTableController];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsNavController];

self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor colorWithHexString:kColorGrayLight];
self.searchController.searchBar.translucent = NO;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, [self.view bounds].size.width, 44.0);
self.searchController.searchBar.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;

//present the search display controller within the confines of this class's table view
self.definesPresentationContext = YES;

// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;

ここに画像の説明を入力

4

5 に答える 5

11

sectionIndex の背景色を変更するだけです。

迅速:

    tableView.sectionIndexBackgroundColor = UIColor.clearColor()
于 2016-05-30T10:23:56.987 に答える
1

最後に、この質問に対する受け入れ可能な解決策を思いつきました。この回答は、上記の私の質問に基づいています (ここで変更されたものを除いたすべての同じコードを使用しています)。ここで注意すべき主なことは、@andrewbuilder が正しかったということです。検索バーをテーブル ヘッダー ビューとして追加すると、検索バーがスクロール時に移動することが保証されます。他のテーブル ヘッダー ビューと同じように機能します。

私が最終的にやったのは、物事の組み合わせでした。私のアプローチで行った主な変更は、メイン コントローラーが検索コントローラーを表示する方法を変更したことです。具体的には、私はからに行きましself.definesPresentationContext = YES;self.definesPresentationContext = NO;

これが行うことは、基本的に検索コントローラーをメイン画面内ではなく、メイン画面の上に表示することです。これにより、1 つではなく 2 つのビュー コントローラーのレイアウトを処理する必要があるという事実で、設計上のトレードオフが生じます。しかし、それは私が大丈夫だったものです。

次に行ったのUISearchBarは、メイン ビュー コントローラーでへの強力なポインターを作成することでしたsearchBar。次に、そのプロパティを検索コントローラーの searchBar と等しくなるように設定し、次のようにサブビューに追加しました。

self.searchBar = self.searchController.searchBar;
[self.view addSubview:self.searchBar];

これを正しく表示するには、テーブルの x 位置が検索バーのフレームの下部と同じであることを確認する必要がありました。コード、Interface Builder、または両方の組み合わせなど、さまざまな方法でこれを行うことができます。正しい選択はあなたに任せます。

繰り返しになりますが、ここでのトレードオフは、2 つのビュー コントローラーをセットアップして、同じ画面ではないにもかかわらず、同じ画面のように見せる必要があることです (ナビゲーション バーのカスタマイズ、セグエの処理など)。これは、私と私のチームが進んで受け入れた必要悪のようです。ここから、これをすべて機能させるための次のステップは、edgesForExtendedLayout両方のビュー コントローラー (およびcontentInsetテーブル ビュー) のプロパティをいじることでした。そのコードは、おそらく設計によって異なるため、ここでは記述しません。しかし、それはそれの要点です。

もっと簡単な解決策を見つけたら、このスレッドに気軽に追加してください。私は広範囲に検索しましたUISearchControllerが、非常に新しいコントロールであるため、ドキュメントや情報 (およびその癖) はあまりありません。

于 2015-03-10T19:00:48.317 に答える
1

検索バーを含むテーブル ビューを作成する際にも同様の懸念がありました。

次のプロパティの外観を変更してみてください...これは、適切な解決策を見つけるのに役立つ場合があります。

Interface Builder / Storyboard を使用している場合は、属性インスペクターで検索バーに次のプロパティを設定します。

  • 検索スタイル = 最小限
  • バーのスタイル = デフォルト
  • 半透明 = はい
  • バーの色合い = デフォルト

ストーリーボード属性インスペクター

もちろん、これらのプロパティはコードで設定できます。

self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchController.searchBar.barStyle = UIBarStyleDefault;
self.searchController.searchBar.translucent = NO;
//  By not setting the barTintColor property, it remains default
//  self.searchController.searchBar.barTintColor = [UIColor yourColor];

次に、Table View Controller コードで、次のUITableViewプロパティを設定します。

//  Prepare custom colours as needed
UIColor *brightBlueColour = [UIColor colorWithRed:0.40 green:0.90 blue:1.00 alpha:1.00];

//  Set section index properties for your table view
[yourTableView setSectionIndexColor:[UIColor brownColor]];
[yourTableView setSectionIndexTrackingBackgroundColor:[UIColor brightBlueColor]];
if ([yourTableView respondsToSelector:@selector(sectionIndexBackgroundColor)])
    [yourTableView setSectionIndexBackgroundColor:[UIColor clearColor]];

//  Set additional properties for background colour
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView *viewBackground = [[UIView alloc] initWithFrame:frame];
[viewBackground setBackgroundColor:self.brightBlueColour];
[yourTableView addSubview:viewBackground];

この組み合わせは、私の意見ではより受け入れやすい外観を提供し、検索バー付きのテーブル ビューを使用するアプリでこれを使用します。

于 2015-03-09T16:00:50.640 に答える
0

最も汚れたシンプルなソリューション (希望どおりに動作します) は、2 つ目の tableView を作成するだけです。いいね(Swift 3):

    tableView = UITableView(frame: CGRect.zero, style: .grouped)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(tableView)
    let cnt1 = NSLayoutConstraint(item: tableView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
    let cnt2 = NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
    let cnt3 = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0)
    let cnt4 = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: tableView, attribute: .bottom, multiplier: 1, constant: -1)
    constraintTableViewBottom = cnt4
    view.addConstraints([cnt1, cnt2, cnt3, cnt4])

    let tableView2 = UITableView(frame: CGRect.zero, style: .grouped)
    tableView2.sectionIndexColor = .black
    tableView2.sectionIndexBackgroundColor = .clear
    tableView2.delegate = self
    tableView2.dataSource = self
    tableView2.translatesAutoresizingMaskIntoConstraints = false
    tableView2.backgroundColor = .clear
    view.addSubview(tableView2)
    let cnt5 = NSLayoutConstraint(item: tableView2, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)
    let cnt6 = NSLayoutConstraint(item: tableView2, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
    let cnt7 = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: tableView2, attribute: .trailing, multiplier: 1, constant: 0)
    let cnt8 = NSLayoutConstraint(item: tableView2, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 13)
    tableView2.addConstraint(cnt8)
    view.addConstraints([cnt5, cnt6, cnt7])

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    if tableView == self.tableView { return nil }
    if searchController.isActive && searchController.searchBar.text != "" {
        return filteredCells.keys.map { return sections[$0] }
    }
    return sections
}

func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: index), at: .top, animated: true)
    return sections.index(of: title)!
}

final func numberOfSections(in tableView: UITableView) -> Int {
    if tableView != self.tableView { return 0 }
    return sections.count
}

そして例えば

final func updateSearchResults(for searchController: UISearchController) {
    filterCells(searchText: searchController.searchBar.text!)
    tableView.reloadSectionIndexTitles()
}
于 2016-09-17T16:36:06.173 に答える