私は そのとして持っUITableViewController
ています。また、それをそのとして、それをそのとして初期化しました。これまでのところ、すべてがほぼ機能しています。UISearchBar
tableHeaderView
tableView
UISearchDisplayController
UISearchBar
searchBar
UITableViewController
contentsController
問題は、に設定されUITableView
ているセルがあることです。何が起こるかです:accessoryType
UITableViewCellAccessoryDetailDisclosureButton
- まず、すべてが適切に表示されます。
- ユーザーは内をタップし
UISearchBar
ます。 - はメインテーブルの
UISearchDisplayController
上に暗いオーバーレイを作成し、メインテーブルのインデックス(のようにsectionIndexTitlesForTableView
)を非表示にします。 - この時点でユーザーがキーボードを非表示にするとします(標準キーボードのiPadの「キーボードを非表示」ボタンを押します)。
- ユーザーはまだ何も入力していないので、
UISearchBar
によって追加された暗いオーバーレイの下にありますが、メインテーブルは引き続き表示されますUISearchDisplayController
。 - キーボードを非表示にすると、メインテーブルがより多く表示され、メインテーブルがより多くのセルをロードするようになります。
- ここで問題があります。メインテーブルのインデックスが非表示になっているときにこれらのセルが読み込まれるため、開示ボタンが右に表示されすぎます(少なくとも他のセルと比較して)。
- さらに、ユーザーが検索をキャンセルすると、それらのセルが再ロードされず、インデックスの下に開示ボタンが表示される場合があります(これが再び表示されます)。
私はこれを回避する方法に迷っています。私が考えることができる唯一のオプションは、開示ボタンに対応するUIViewを見つけて手動で移動することですが、UIViewを見つけるだけでも厄介なハックが必要なため、それは信じられないほどハッキーに思えます。これをより良い方法で修正する方法についての提案は大歓迎です!
実行可能な最小限の例
以下は最小限の例です。新しいXCodeプロジェクトを開始し、ARC、iPadのみを有効にして、AppDelegateのコンテンツを以下に置き換えます。最小限の例のために、メインテーブルにセルを強制的にリロードすることに注意してくださいsearchDisplayController:willShowSearchResultsTableView
。そうしないと、メインテーブルがセルをキャッシュし、問題が表示されません(実際のアプリケーションでは、メインテーブルは他の理由でセルをリロードしています、理由は完全にはわかりませんが、もちろん、メインテーブルがいつでもセルをリロードしても問題ないはずです。)
問題が発生していることを確認するには、コードを実行し、検索ボックスに何かを入力して(「検索結果0 .. 5」と表示されます)、検索をキャンセルします。メインテーブルの開示ボタンは、インデックスの横ではなく、下に表示されるようになりました。
以下は単なるコードです:
@interface AppDelegate ()
@property (nonatomic, strong) UITableViewController* mainTableController;
@property (nonatomic, strong) UISearchDisplayController* searchDisplay;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITableViewController* tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UITableView* tableView = [tableViewController tableView];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[tableView setDataSource:self];
[self setMainTableController:tableViewController];
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)]; // Width set automatically
[tableView setTableHeaderView:searchBar];
UISearchDisplayController* searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
contentsController:tableViewController];
[searchDisplay setSearchResultsDataSource:self];
[searchDisplay setDelegate:self];
[self setSearchDisplay:searchDisplay];
[[self window] setRootViewController:tableViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView != [[self searchDisplay] searchResultsTableView]) {
return 26;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView != [[self searchDisplay] searchResultsTableView]) {
return 10;
} else {
return 5;
}
}
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
// The problem arises only if the main table view needs to reload its data
// In this minimal example, we force this to happen
[[[self mainTableController] tableView] reloadData];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SearchCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView != [[self searchDisplay] searchResultsTableView]) {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[[cell textLabel] setText:[NSString stringWithFormat:@"%c%d", 'A' + [indexPath section], [indexPath row]]];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
return cell;
} else {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
[[cell textLabel] setText:[NSString stringWithFormat:@"Search result %d", [indexPath row]]];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
return cell;
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (tableView != [[self searchDisplay] searchResultsTableView]) {
return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
} else {
return nil;
}
}