stackoverflow で同等の質問が見つからなかったので、その問題に関する独自の質問を投稿します (理解できない点があれば教えてください)。
問題:
tableView
カスタムで、AZ のセクション ヘッダー (contacts.app など) でインデックス化された一般的なものを使用しますUITableViewController
。オーバーライドtableView:viewForHeaderInSection:
して、独自のセクション ヘッダー ビューを提供します。
ユーザーがテーブルを上下にスクロールすると、tableView の上部または下部に表示/非表示になるセクション ヘッダーをリッスンする必要があります (それに応じてカスタム ビューを変更するため)。
必要なことを正確に実行するこれらのメソッド (iOS 6.0 以降で使用可能) をオーバーライドします。
tableView:didEndDisplayingHeaderView:forSection:
tableView:willDisplayHeaderView:forSection:
2. tableView を上下にスクロールするときに呼び出されることはありませんが、セクション ヘッダーが画面から消えるたびにメソッド 1 が呼び出されます。
なぜ一方が呼び出され、もう一方が呼び出されないのかわかりません。これはアップルのバグですか、それとも何が間違っていますか?
類似の質問:
ここで同様の質問を見つけました テーブルビューのカスタマイズ中にメソッド「willDisplayFooterView」を呼び出す方法は? 1つの答えは次のようなものでした:
これらは、iOS 6.0 以降でのみ呼び出され、他のヘッダー/フッターのタイトル/ビュー メソッドも実装している場合にのみ呼び出されます。ヘッダーまたはフッターを提供している場合、これらを呼び出す必要はありません
その答えを正しく理解しているかどうかはわかりませんが、そうであれば、サブクラスでどのメソッドが実装されているかが重要なようです。
コード:
my の空でないオーバーライドされたデリゲートとデータソース メソッドのみを投稿しますtableViewController
。
tvcA.m
@implementation tvcA
...
#pragma mark - Table view data source
- (NSArray *) sectionIndexTitlesForTableView : (UITableView *) tableView
{
// returns sectionIndexArray with alphabet for example
}
- (UIView *) tableView : (UITableView *) tableView
viewForHeaderInSection : (NSInteger) section
{
MNSectionHeaderView* headerView = [[MNSectionHeaderView alloc] initWithFrame : CGRectMake(0, 0, 260, 22)];
return headerView;
}
- (NSInteger) tableView : (UITableView *) tableView
sectionForSectionIndexTitle : (NSString *) title
atIndex : (NSInteger) index {...}
...
@end
tvcB.h (tvcA から継承)
@interface tvcB : tvcA
...
@end
tvcB.m
@implementation tvcB
...
#pragma mark - Table view data source
- (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView {...}
- (NSInteger) tableView : (UITableView *) tableView
numberOfRowsInSection : (NSInteger) section {...}
- (UITableViewCell*) tableView : (UITableView *) tableView
cellForRowAtIndexPath : (NSIndexPath *) indexPath {...}
- (CGFloat) tableView : (UITableView *) tableView
heightForHeaderInSection : (NSInteger) section {...}
- (UIView *) tableView : (UITableView *) tableView
viewForHeaderInSection : (NSInteger) section {....}
#pragma mark - Table view delegate
- (void) tableView : (UITableView*) tableView
willDisplayHeaderView : (UIView*) view
forSection : (NSInteger) section {
// custom configurations
}
- (void) tableView : (UITableView*) tableView
didEndDisplayingHeaderView : (UIView*) view
forSection : (NSInteger) section {
// custom configurations
}
- (void) tableView : (UITableView *) tableView
didSelectRowAtIndexPath : (NSIndexPath *) indexPath {...}
...
@end