7

stackoverflow で同等の質問が見つからなかったので、その問題に関する独自の質問を投稿します (理解できない点があれば教えてください)。

問題:

tableViewカスタムで、AZ のセクション ヘッダー (contacts.app など) でインデックス化された一般的なものを使用しますUITableViewController。オーバーライドtableView:viewForHeaderInSection:して、独自のセクション ヘッダー ビューを提供します。

ユーザーがテーブルを上下にスクロールすると、tableView の上部または下部に表示/非表示になるセクション ヘッダーをリッスンする必要があります (それに応じてカスタム ビューを変更するため)。

必要なことを正確に実行するこれらのメソッド (iOS 6.0 以降で使用可能) をオーバーライドします。

  1. tableView:didEndDisplayingHeaderView:forSection:

  2. 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
4

2 に答える 2

10

私は今それを手に入れたと思います。

上記のSimilar Questionはすでに答えを出していますが、私は正しくありませんでした:

[...] ヘッダーまたはフッターを提供している場合、これらを呼び出す必要はありません

(「これら」とは、私が上でオーバーライドした 2 つのメソッドを意味していたのかもしれません。)

method の代わりに独自のセクション ヘッダーを提供しているため 、セクション ヘッダーが画面に入るたびtableView:willDisplayHeaderView:forSection:に、他のオーバーライド メソッド:が呼び出されます。tableView:viewForHeaderInSection:

最後に、最後に述べた方法で必要な構成を行います。

于 2013-06-18T10:48:57.457 に答える
0

私の場合、何らかの理由で、tableView:viewForHeaderInSection:すべてのテーブルのすべてのセクションに対して呼び出されていました(多くのテーブルを持つ scrollView があります)。だからあなたの解決策は私にとってはうまくいきませんでした。ただし、VC がポップされたときに最初に表示されるセクションを知る必要があるだけだったので、次の優れたソリューションを思い付きました。

    NSInteger firstVisibleSection = 0;
    for (int i = 0; i < [_board.swimlanes count]; i++)
    {
        CGRect sectionRect = [currentTable rectForSection:i];
        BOOL containsPoint = CGRectContainsPoint(sectionRect, currentTable.contentOffset);
        if (containsPoint)
        {
            firstVisibleSection = i;
            break;
        }
    }

同じ問題を抱えている人に役立つかもしれません:)

于 2014-03-28T17:51:07.657 に答える