我已经构建了一个自定义IKImageBrowserCell,它在IKImageBrowser中显示我的图像而没有任何问题。
我想尝试覆盖内置的IKImageBrowser删除图像功能。目前“开箱即用”您可以选择图像或多个图像,然后按BACKSPACE进行删除。
我想添加一个NSButton或类似功能,以便在每个图像上启用相同的功能。
我添加了以下代码,以便在选择IKImageBrowserCell时显示删除图标:
- (CALayer *) layerForType:(NSString*) type {
CGColorRef color;
//retrieve some usefull rects
NSRect frame = [self frame];
NSRect imageFrame = [self imageFrame];
NSRect relativeImageFrame = NSMakeRect(imageFrame.origin.x - frame.origin.x, imageFrame.origin.y - frame.origin.y, imageFrame.size.width, imageFrame.size.height);
if(type == IKImageBrowserCellForegroundLayer){
//no foreground layer on place holders
if([self cellState] != IKImageStateReady)
return nil;
//create a foreground layer that will contain several childs layer
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
if([self isSelected]){
//add a delete icon
CALayer *deleteLayer = [CALayer layer];
[deleteLayer setContents:(id)deleteImage()];
deleteLayer.frame = CGRectMake(relativeImageFrame.size.width-14, (relativeImageFrame.origin.y+relativeImageFrame.size.height)-14, 28, 28);
[layer addSublayer:deleteLayer];
}
}
}
这很好用,但显然只是静态图像。有没有什么方法可以尝试通过点击此删除图标获取事件,然后将选定的单元格索引返回到IKImageBrowser,以便调用它的removeItemsFromIndex:方法?我被困了!
答案 0 :(得分:0)
myIKImageBrowserView.selectionIndexes()返回当前所选单元格的NSIndexSet - 您可以在删除图层之前使用它来调用removeItemsFromIndex方法