Macos 如何在ListView中添加子视图?
我正在开发我的第一个MAC应用程序,我下载了一个 我必须在cell xib上添加一个按钮和背景图像,并将它们与控制器绑定 当点击按钮时,我被设置为该单元格的高度比其他单元格的高度大得多。就这样,, 工作很好 但现在我想在witch单元格打开后开发,我想在它上面添加一些额外的contain(Controller),那么如何使用给定的示例呢? 请帮我提出一些建议如何做。Macos 如何在ListView中添加子视图?,macos,cocoa,nscollectionview,Macos,Cocoa,Nscollectionview,我正在开发我的第一个MAC应用程序,我下载了一个 我必须在cell xib上添加一个按钮和背景图像,并将它们与控制器绑定 当点击按钮时,我被设置为该单元格的高度比其他单元格的高度大得多。就这样,, 工作很好 但现在我想在witch单元格打开后开发,我想在它上面添加一些额外的contain(Controller),那么如何使用给定的示例呢? 请帮我提出一些建议如何做。 对于Ex-like-before,请单击按钮 在按下按钮后,我想发展成这样 你写的 我必须在cell xib上添加一个按钮和背景
对于Ex-like-before,请单击按钮 在按下按钮后,我想发展成这样
你写的 我必须在cell xib上添加一个按钮和背景图像,并将它们与控制器绑定 听起来您好像已经对
PXListViewCell
进行了子类化——为了方便起见,让我们调用您的子类TemplateListViewCell
——并添加了一个xib
,从中加载TemplateListViewCell
的实例
+[PXListViewCell cellLoadedFromNibNamed:bundle:reusableIdentifier:]
此外,在TemplateListViewCell.xib
中还有一个[至少一个]按钮
你写的 当点击按钮时,我被设置为该单元格的高度比其他单元格大得多。完成了,工作很好 听起来这个按钮在
TemplateListViewCell
上有一个方法作为其操作,例如
- (IBAction)toggleDetail:(id)sender
{
//Code to grow or shrink the height of [self frame].
//...
}
在我实现-toggleDetail
的方法中,需要对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:
的方法
你写的 但现在我想在witch单元格打开后开发,我想在它上面添加一些额外的contain(Controller),那么如何使用给定的示例呢?请帮我给一些建议如何做 听起来您希望
TemplateListViewCell
的实例在展开时包含额外的视图
将此代码放到-[TemplateListViewCell toggleDetail]
中似乎很有诱惑力,但这不会像我们希望的那样奏效。问题是,我们需要处理这样的情况:扩展的单元格已从视图中滚出,然后又滚回视图中
为了做到这一点,我们需要有一个扩展的概念,该概念在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 layoutCells]调用-[PXListView setCell:expandedAtRow:
- (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:][/code>:
- (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.
设置\u在-[PXListViewCell prepareforeuse]中将扩展为NO
:
- (void)prepareForReuse
{
_dropHighlight = PXListViewDropNowhere;
_expanded = NO;
}
- (void)prepareForReuse
{
//...
[super prepareForReuse];
}
注意:在与PXListView
一起分发的情况下,-[MyListViewCell prepareForReuse]
的实现无法调用[super prepareForReuse]
。确保此调用是在[TemplateListViewCell Prepareforuse]
中进行的:
- (void)prepareForReuse
{
_dropHighlight = PXListViewDropNowhere;
_expanded = NO;
}
- (void)prepareForReuse
{
//...
[super prepareForReuse];
}
需要对-[TemplateListViewCell toggleDetail:
进行一项更改。线路
[self setExpanded:isExpanded];
需要由
[[[self listView] delegate] listView:[self listView] setExpanded:isExpanded atRow:[self row]];
一旦您设置了PXListView
的委托以正确处理新的委托方法,就可以在子类TemplateListViewCell
中重写[PXListViewCell setExpanded:][/code>:
- (void)setExpanded:(BOOL)expanded
{
if (expanded) {
//add detail subviews
} else {
//remove detail subviews
}
[super setExpanded:expanded];
}
用自己的代码替换//添加细节子视图
,该代码以编程方式添加所需的细节子视图,并用代码替换//删除细节子视图
,以删除所需的细节子视图,首先检查它们是否存在
你写的
我想添加一些额外的包含(控制器)
听起来您想将视图控制器而不是视图添加到TemplateListViewCell
。为此,请使用NSBox
并将该框的contentView
设置为视图控制器的视图。(有关详细信息,请参阅。)
如果您计划仅在展开的TemplateListViewCell
上的NSBox
中显示单个视图控制器的视图,则可以(1)向引用视图控制器的TemplateListViewCell添加属性,以及(2)将一个NSBox
添加到TemplateListViewCell
xib中,并在[单元格设置展开:是]
上将其内容视图设置到相应视图控制器的视图中,在[单元格设置展开:否]
上将其内容视图设置为nil
您的第二段不清楚。您是否有一个按钮可以使列表视图变大?这已经起作用了吗?沃