あなたが書く
セルxibに1つのボタンと背景画像を追加し、それらをコントローラーにバインドする必要があります
サブクラス化したようですPXListViewCell
-- 便宜上、サブクラスを呼び出しましょう -- のインスタンスがロードされる元の をTemplateListViewCell
追加しましたxib
TemplateListViewCell
+[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)NSBox
をTemplateListViewCell
xib に追加して、それcontentView
を適切なビュー コントローラーのビューに設定[cell setExpanded:YES]
します。 contentView をnil
on に設定し[cell setExpanded:NO]
ます。