3

初めての MAC アプリケーションを開発しています 。PxListViewの例を 1 つダウンロードしました

そして、セルxibに1つのボタンと背景画像を追加し、それらをコントローラーにバインドする必要があります。ボタンをクリックすると、そのセルの高さが他のセルよりもはるかに大きく設定されました。それが完了し、正常に動作します。

しかし、今はそのセルで魔女セルが開いているように開発したいので、そのセルに追加のコンテイン (コントローラー) を追加したいので、与えられた例を使用してどのように可能になりますか? plsは、それがどのように行われるかを提案するのを手伝ってくれます.
ボタンをクリックする前の例の場合 ここに画像の説明を入力

ボタンのひよこの後、次のように開発したい

ここに画像の説明を入力

4

1 に答える 1

2

あなたが書く

セルxibに1つのボタンと背景画像を追加し、それらをコントローラーにバインドする必要があります

サブクラス化したようですPXListViewCell-- 便宜上、サブクラスを呼び出しましょう -- のインスタンスがロードされる元の をTemplateListViewCell追加しましたxibTemplateListViewCell

+[PXListViewCell cellLoadedFromNibNamed:bundle:reusableIdentifier:]

また、 には[少なくとも1つ]ボタンがありTemplateListViewCell.xibます。

あなたが書く

ボタンをクリックすると、そのセルの高さが他のセルよりもはるかに大きく設定されました。それが完了し、正常に動作します

このボタンには、アクションとして次のようなメソッドがあるTemplateListViewCellようです

- (IBAction)toggleDetail:(id)sender
{
    //Code to grow or shrink the height of [self frame].  
    //...
}

を実装するための私のアプローチでは、ファイル-toggleDetailへの 2 つの変更が必要でした。PXListView

1. プロトコル メソッドの追加

- (void)listView:(PXListView *)aListView setHeight:(CGFloat)height ofRow:(NSUInteger)row;

PXListViewDelegateプロトコルに。

2. プロパティの追加

@property (nonatomic, assign) BOOL expanded;

PXListViewCell

私の実装は次の-toggleDetailようになります。

- (IBAction)toggleDetail:(id)sender
{
    BOOL wasExpanded = [self expanded];

    NSRect oldFrame = [self frame];
    CGFloat oldHeight = oldFrame.size.height;
    CGFloat newHeight = oldHeight;

    CGFloat heightIncrement = 0.0f;
    if (wasExpanded) {
        heightIncrement = -80.0f; //use whatever value is appropriate
    } else {
        heightIncrement = 80.0f;  //use whatever value is appropriate
    }
    newHeight += heightIncrement;

    [[[self listView] delegate] listView:[self listView] setHeight:newHeight ofRow:[self row]];
    [[self listView] reloadData];

    BOOL isExpanded = !wasExpanded;
    [self setExpanded:isExpanded];
}

[[self listView] reloadRowAtIndex:[self row]];の代わりに使用する方が良いように思えるかもしれません[[self listView] reloadData]が、残念ながら、これは機能しません。ユーザーが詳細を非表示にした場合 (セルを垂直方向に縮小した場合)、画面に表示されるはずの新しいセルが表示されません。

あなたが書く

それが完了し、正常に動作します。

に類似したメソッドを正常に実装できたようです-[TemplateListViewCell toggleDetail:]

あなたが書く

しかし今、私は魔女セルがそのセルで開いているように開発したいので、そのセルに追加のコンテイン (コントローラー) を追加したいので、与えられた例を使用してどのようにそれを可能にしますか? plsは、それがどのように行われるかを提案するのを手伝ってくれます.

のインスタンスTemplateListViewCellが展開されている場合、追加のビューを含める必要があるようです。

このコードを に入れたくなるように思えるかもしれませ-[TemplateListViewCell toggleDetail]んが、期待どおりには機能しません。問題は、展開されたセルがスクロールされてビューから外れ、スクロールしてビューに戻った場合を処理する必要があることです。

これを正しく行うには、サブクラス インスタンスの使用を超えて持続する Expanded の概念が必要です。それ自体またはそのデリゲート PXListViewCellで展開を追跡する必要があります。PXListView

PXListViewより優れた (しかしあまり便利ではない) 設計は、この情報自体を追跡するように思われます。ただし、この質問のために、デリゲートでセル拡張を追跡する方法を示します。これを行うために、PXListViewDelegateプロトコルを拡張し、ファイルに他の変更を加えていPXListViewます。

1. メソッドの追加

- (void)listView:(PXListView *)aListView setExpanded:(BOOL)expanded atRow:(NSUInteger)row;
- (BOOL)listView:(PXListView *)aListView expandedAtRow:(NSUInteger)row;

PXListViewDelegate

2. メソッドの追加

- (void)setCell:(PXListViewCell *)cell expandedAtRow:(NSUInteger)row
{
    if ([[self delegate] respondsToSelector:@selector(listView:expandedAtRow:)]) {
        [cell setExpanded:[[self delegate] listView:self expandedAtRow:row]];
    }
}

PXListView

3.-[PXListView setCell:expandedAtRow:]からの 呼び出し-[PXListView layoutCells]

- (void)layoutCells
{   
    //Set the frames of the cells
    for(id cell in _visibleCells)
    {
        NSInteger row = [cell row];
        [cell setFrame:[self rectOfRow:row]];


        [self setCell:cell expandedAtRow:row];


        [cell layoutSubviews];
    }

    NSRect bounds = [self bounds];
    CGFloat documentHeight = _totalHeight>NSHeight(bounds)?_totalHeight:(NSHeight(bounds) -2);

    //Set the new height of the document view
    [[self documentView] setFrame:NSMakeRect(0.0f, 0.0f, NSWidth([self contentViewRect]), documentHeight)];
}

とから-[PXListView layoutCell:atRow:]

- (void)layoutCell:(PXListViewCell*)cell atRow:(NSUInteger)row
{
    [[self documentView] addSubview:cell];
    [cell setFrame:[self rectOfRow:row]];

    [cell setListView:self];
    [cell setRow:row];
    [cell setHidden:NO];

    [self setCell:cell expandedAtRow:row];
}

4.に 設定: _expanded_NO-[PXListViewCell prepareForReuse]

- (void)prepareForReuse
{
    _dropHighlight = PXListViewDropNowhere;
    _expanded = NO;
}

:サンプルPXListViewCellサブクラス ではMyListViewCellと共に配布されPXListView、 の実装は の-[MyListViewCell prepareForReuse]呼び出しに失敗します[super prepareForReuse]。この呼び出しが で行われていることを確認してください[TemplateListViewCell prepareForReuse]:

- (void)prepareForReuse
{
    //...
    [super prepareForReuse];
}

に 1 つの変更を加える必要があります-[TemplateListViewCell toggleDetail:]。この線

[self setExpanded:isExpanded];

に置き換える必要があります

[[[self listView] delegate] listView:[self listView] setExpanded:isExpanded atRow:[self row]];

PXListView新しいデリゲート メソッドを適切に処理するように のデリゲートを設定したら[PXListViewCell setExpanded:]、サブクラスでオーバーライドする準備が整いTemplateListViewCellます。

- (void)setExpanded:(BOOL)expanded
{
    if (expanded) {
        //add detail subviews
    } else {
        //remove detail subviews
    }
    [super setExpanded:expanded];
}

//add detail subviews必要な詳細サブビューをプログラムで追加する独自のコードに置き換え、必要//remove detail subviewsな詳細サブビューを削除するコードに置き換えて、それらが最初に存在することを確認します。

あなたが書く

その上に追加のコンテイン (コントローラー) を追加したい

ビューではなくビューコントローラーを に追加したいようですTemplateListViewCell。これを行うには、 を使用しNSBox、ボックスcontentViewをビュー コントローラーの に設定しますview。(詳細については、この回答を参照してください。)

NSBox展開された の に単一のビュー コントローラーのビューを表示するTemplateListViewCellだけの場合は、(1) ビュー コントローラーを参照するプロパティを追加しTemplateListViewCell、(2)NSBoxTemplateListViewCellxib に追加して、それcontentViewを適切なビュー コントローラーのビューに設定[cell setExpanded:YES]します。 contentView をnilon に設定し[cell setExpanded:NO]ます。

于 2012-12-28T21:41:09.020 に答える