一个 NSTableviewColumn 中的多个 NSTableCellView Cocoa OSX
Multiple NSTableCellView in one NSTableviewColumn Cocoa OSX
我正在尝试在一列中创建一个包含不同单元格的 NSTableview
。我在一列中设置两个 NSTableCellView
,如下所示:
并像这样设置数据代码:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:@"BugColumn"] )
{
NSMutableDictionary *dic =(NSMutableDictionary*)[self.ArraRecentDataLoad objectAtIndex:row];
if([[dic valueForKey:@"type"] isEqualToString:@"image/png"] || [[dic valueForKey:@"type"] isEqualToString:@"image/jpeg"])
{
ImageCell *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
cellView = [[ImageCell alloc] initWithFrame:NSMakeRect(0, 0, 316, 313)];
// This allows the cell to be reused.
cellView.identifier =tableColumn.identifier;
NSColor *orangeColor = [NSColor colorWithCGColor:[NSColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1.0f].CGColor];
// Convert to CGColorRef
NSInteger numberOfComponents = [orangeColor numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[orangeColor colorSpace] CGColorSpace];
[orangeColor getComponents:(CGFloat *)&components];
CGColorRef orangeCGColor = CGColorCreate(colorSpace, components);
[cellView.bgCellview setWantsLayer:YES];
cellView.bgCellview.layer.masksToBounds= YES;
cellView.bgCellview.layer.borderWidth=2;
// Set border
cellView.bgCellview.layer.borderColor = orangeCGColor;
// Clean up
CGColorRelease(orangeCGColor);
return cellView;
}
else
{
HomeCell *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
NSColor *orangeColor = [NSColor colorWithCGColor:[NSColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1.0f].CGColor];
// Convert to CGColorRef
NSInteger numberOfComponents = [orangeColor numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[orangeColor colorSpace] CGColorSpace];
[orangeColor getComponents:(CGFloat *)&components];
CGColorRef orangeCGColor = CGColorCreate(colorSpace, components);
[cellView.bgview setWantsLayer:YES];
cellView.bgview.layer.masksToBounds= YES;
cellView.bgview.layer.borderWidth=2;
// Set border
cellView.bgview.layer.borderColor = orangeCGColor;
// Clean up
CGColorRelease(orangeCGColor);
return cellView;
}
}
return nil;
}
但是在我的 xib
中,如果我拖动第二个单元格 (imageCell),那么我的应用程序会因无法识别 select 而崩溃。如果我像上面的屏幕截图那样做,那么我无法加载第二个单元格并且 NSTableview
.
中有白色 space
我做了很多 RND,但我无法找到在单个列中使用不同单元格并根据条件数据加载的正确方法。
注:
如果我在第一个条件中删除 alloc
单元格:
cellView = [[ImageCell alloc] initWithFrame:NSMakeRect(0, 0, 316, 313)];
cellView.identifier =tableColumn.identifier;
然后显示第一行的预定义标签,而不是第二行 ImageCell
预定义,如 iOS。
两个单元格视图需要有不同的标识符。您将必须至少手动分配其中一个。您不能让 Xcode 自动分配两者。 (我什至不确定 Xcode 是否会让你这样配置。)
然后,您需要在调用[tableView makeViewWithIdentifier:... owner:self]
时使用正确的标识符。这些调用中最多有一个可以使用 table 列的标识符。另一个必须使用手动分配的。
如果您将两个单元格视图都放在 NIB 中(使用单独的标识符)并使用 [tableView makeViewWithIdentifier:... owner:self]
获取它们,那么您不需要以编程方式 alloc/init 一个(并且您不应该这样做) .如果您更喜欢以编程方式 alloc/init 一个,则必须为其分配一个不同的标识符。同样,您不能有两个具有相同标识符的独立单元格视图类型。
更新:让我以更简单的形式再试一次。这样做:
- 在 NIB 中,select Image Cell 视图。在身份检查器上,将标识符设置为 "ImageCell".
- 在您的代码中,对于图像分支,执行
ImageCell *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
而不是使用 table 列标识符。
- 删除以下两条分配
ImageCell
class 的新实例并设置其 identifier
属性. 的语句
因此,代码应如下所示:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:@"BugColumn"] )
{
NSMutableDictionary *dic =(NSMutableDictionary*)[self.ArraRecentDataLoad objectAtIndex:row];
if([[dic valueForKey:@"type"] isEqualToString:@"image/png"] || [[dic valueForKey:@"type"] isEqualToString:@"image/jpeg"])
{
ImageCell *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
NSColor *orangeColor = [NSColor colorWithCGColor:[NSColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1.0f].CGColor];
// ...
首先查看从 nib 中查看基础 table 视图。然后在 viewfortable 列中放置图像条件,然后首先检查视图(图像单元格或主单元格使其简单 NSView)可用于图像类型,然后使用它或使用 makeviewusingidentifire 或任何你使用的分配和 return那个观点。在第二个条件下,你已经为此做了同样的事情。
每次调用此方法时,它都会首先检查它想要哪种类型的视图(在此特定情况下是图像单元格或主单元格),然后检查它是否在内存中可用,如果不可用则使用它,然后分配并使用它。
如果有任何问题,请告诉我。
抱歉英语不好
我正在尝试在一列中创建一个包含不同单元格的 NSTableview
。我在一列中设置两个 NSTableCellView
,如下所示:
并像这样设置数据代码:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:@"BugColumn"] )
{
NSMutableDictionary *dic =(NSMutableDictionary*)[self.ArraRecentDataLoad objectAtIndex:row];
if([[dic valueForKey:@"type"] isEqualToString:@"image/png"] || [[dic valueForKey:@"type"] isEqualToString:@"image/jpeg"])
{
ImageCell *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
cellView = [[ImageCell alloc] initWithFrame:NSMakeRect(0, 0, 316, 313)];
// This allows the cell to be reused.
cellView.identifier =tableColumn.identifier;
NSColor *orangeColor = [NSColor colorWithCGColor:[NSColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1.0f].CGColor];
// Convert to CGColorRef
NSInteger numberOfComponents = [orangeColor numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[orangeColor colorSpace] CGColorSpace];
[orangeColor getComponents:(CGFloat *)&components];
CGColorRef orangeCGColor = CGColorCreate(colorSpace, components);
[cellView.bgCellview setWantsLayer:YES];
cellView.bgCellview.layer.masksToBounds= YES;
cellView.bgCellview.layer.borderWidth=2;
// Set border
cellView.bgCellview.layer.borderColor = orangeCGColor;
// Clean up
CGColorRelease(orangeCGColor);
return cellView;
}
else
{
HomeCell *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
NSColor *orangeColor = [NSColor colorWithCGColor:[NSColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1.0f].CGColor];
// Convert to CGColorRef
NSInteger numberOfComponents = [orangeColor numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[orangeColor colorSpace] CGColorSpace];
[orangeColor getComponents:(CGFloat *)&components];
CGColorRef orangeCGColor = CGColorCreate(colorSpace, components);
[cellView.bgview setWantsLayer:YES];
cellView.bgview.layer.masksToBounds= YES;
cellView.bgview.layer.borderWidth=2;
// Set border
cellView.bgview.layer.borderColor = orangeCGColor;
// Clean up
CGColorRelease(orangeCGColor);
return cellView;
}
}
return nil;
}
但是在我的 xib
中,如果我拖动第二个单元格 (imageCell),那么我的应用程序会因无法识别 select 而崩溃。如果我像上面的屏幕截图那样做,那么我无法加载第二个单元格并且 NSTableview
.
我做了很多 RND,但我无法找到在单个列中使用不同单元格并根据条件数据加载的正确方法。
注:
如果我在第一个条件中删除 alloc
单元格:
cellView = [[ImageCell alloc] initWithFrame:NSMakeRect(0, 0, 316, 313)];
cellView.identifier =tableColumn.identifier;
然后显示第一行的预定义标签,而不是第二行 ImageCell
预定义,如 iOS。
两个单元格视图需要有不同的标识符。您将必须至少手动分配其中一个。您不能让 Xcode 自动分配两者。 (我什至不确定 Xcode 是否会让你这样配置。)
然后,您需要在调用[tableView makeViewWithIdentifier:... owner:self]
时使用正确的标识符。这些调用中最多有一个可以使用 table 列的标识符。另一个必须使用手动分配的。
如果您将两个单元格视图都放在 NIB 中(使用单独的标识符)并使用 [tableView makeViewWithIdentifier:... owner:self]
获取它们,那么您不需要以编程方式 alloc/init 一个(并且您不应该这样做) .如果您更喜欢以编程方式 alloc/init 一个,则必须为其分配一个不同的标识符。同样,您不能有两个具有相同标识符的独立单元格视图类型。
更新:让我以更简单的形式再试一次。这样做:
- 在 NIB 中,select Image Cell 视图。在身份检查器上,将标识符设置为 "ImageCell".
- 在您的代码中,对于图像分支,执行
ImageCell *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
而不是使用 table 列标识符。 - 删除以下两条分配
ImageCell
class 的新实例并设置其identifier
属性. 的语句
因此,代码应如下所示:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:@"BugColumn"] )
{
NSMutableDictionary *dic =(NSMutableDictionary*)[self.ArraRecentDataLoad objectAtIndex:row];
if([[dic valueForKey:@"type"] isEqualToString:@"image/png"] || [[dic valueForKey:@"type"] isEqualToString:@"image/jpeg"])
{
ImageCell *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
NSColor *orangeColor = [NSColor colorWithCGColor:[NSColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1.0f].CGColor];
// ...
首先查看从 nib 中查看基础 table 视图。然后在 viewfortable 列中放置图像条件,然后首先检查视图(图像单元格或主单元格使其简单 NSView)可用于图像类型,然后使用它或使用 makeviewusingidentifire 或任何你使用的分配和 return那个观点。在第二个条件下,你已经为此做了同样的事情。
每次调用此方法时,它都会首先检查它想要哪种类型的视图(在此特定情况下是图像单元格或主单元格),然后检查它是否在内存中可用,如果不可用则使用它,然后分配并使用它。
如果有任何问题,请告诉我。 抱歉英语不好