从TableViewCell中删除imageView子图层 [英] Remove imageView sublayer from TableViewCell
问题描述
当我有一个图像时,我插入子图层与CAGradientLayer,
...
layer.name = @ 梯度;
[cell.imageView.layer insertSublayer:layer atIndex:0];
当没有ImageView的图像时,我需要删除这个子图层。
我尝试了不同的方法,没有成功。我最后尝试的是
for(CALayer * layer in cell.imageView.layer.sublayers){
if [layer.name isEqualToString:@Gradient]){
[layer removeFromSuperlayer];
}
}
但它给我一个错误:
CoreData:错误:严重的应用程序错误。在对-controllerDidChangeContent:的调用期间,NSFetchedResultsController的委托捕获了异常。 *** Collection< CALayerArray:0xc1502e0>被枚举时发生突变。 with userInfo(null)
抛出异常,正在改变子层
数组的内容,同时使用foreach循环枚举它。这不是层的特殊之处,在枚举任何NSMutableArray时添加/删除对象时会抛出类似的异常。
您有各种选项来解决此问题
解决方案1:在修改数组时立即停止枚举。
for(CALayer * layer in cell.imageView.layer.sublayers){
if([layer.name isEqualToString:@Gradient]){
[layer removeFromSuperlayer];
break;
}
}
解决方案2:不是枚举真正的数组,而是使用它的副本。
NSArray * sublayers = [NSArray arrayWithArray:cell.imageView.layer.sublayers ];
for(CALayer * layer in sublayers){
if([layer.name isEqualToString:@Gradient]){
[layer removeFromSuperlayer];
}
}
解决方案3:
$ b[cell.imageView.layer insertSublayer:layer atIndex:0];
[cell.imageView.layer setValue:layer forKey:@GradientLayer];
检索并删除
CALayer * layer = [cell.imageView.layer valueForKey:@GradientLayer];
[layer removeFromSuperlayer];
[cell.imageView.layer setValue:nil forKey:@GradientLayer];
When I have an image I insert sublayer with CAGradientLayer,
...
layer.name = @"Gradient";
[cell.imageView.layer insertSublayer:layer atIndex:0];
and when there is no image for ImageView I need to remove this sublayer. I tried different ways with no success. The last I tried was
for (CALayer *layer in cell.imageView.layer.sublayers) {
if ([layer.name isEqualToString:@"Gradient"]) {
[layer removeFromSuperlayer];
}
}
but it gives me an error:
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. *** Collection <CALayerArray: 0xc1502e0> was mutated while being enumerated. with userInfo (null)
The exception is thrown because you are changing the contents of the sublayers
array while enumerating it with foreach loop. This is not something special to layers, a similar exception is thrown when you add/remove objects while enumerating any NSMutableArray.
You have various options to solve this issue
Solution 1: Stop enumerating as soon as you modify the array.
for (CALayer *layer in cell.imageView.layer.sublayers) {
if ([layer.name isEqualToString:@"Gradient"]) {
[layer removeFromSuperlayer];
break;
}
}
Solution 2: Do not enumerate the real array, instead use its copy.
NSArray* sublayers = [NSArray arrayWithArray:cell.imageView.layer.sublayers];
for (CALayer *layer in sublayers) {
if ([layer.name isEqualToString:@"Gradient"]) {
[layer removeFromSuperlayer];
}
}
Solution 3: Use key value coding to keep a reference to the gradient layer.
Set it after inserting:
[cell.imageView.layer insertSublayer:layer atIndex:0];
[cell.imageView.layer setValue:layer forKey:@"GradientLayer"];
Retrieve and remove it
CALayer* layer = [cell.imageView.layer valueForKey:@"GradientLayer"];
[layer removeFromSuperlayer];
[cell.imageView.layer setValue:nil forKey:@"GradientLayer"];
这篇关于从TableViewCell中删除imageView子图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!