У меня есть таблица устройств, которые могут быть подключены или нет.если устройство подключено, имеется переключатель включения / выключения (в виде аксессуара) для управления им.если он отключен, переключаемый заменяется символом отключения.Все идет нормально.Теперь я хочу добавить слой, чтобы затемнить ячейку серым, если она отключена, и, очевидно, слой исчезнет, если он подключен.(Для тестирования, я просто подключил эту кнопку к кнопке для переключения состояния. Я выяснил, как разместить слой, но не могу его удалить. Нажмите один - отобразит слой, щелкните два - ничего не делает, нажмите три -делает слой еще темнее и т. д., пока он не станет полностью черным. Блок вызывается, потому что при каждом щелчке я вижу оператор log: greyString равен 1 "или greyString равен 0. Я пробовал [layer setHidden: YES], [layerremoveFromSuperlayer] и layer.backroughColr = clearColor, без эффекта
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NamedUISwitch *mySwitch = [[NamedUISwitch alloc] initWithFrame:CGRectZero];
mySwitch.indexPath = indexPath;
mySwitch.pathRow=indexPath.row;
[mySwitch addTarget:self action:@selector(changeDeviceState:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView=mySwitch;
if ([[plugStates objectAtIndex:indexPath.row] isEqualToString:@"11"]) {
cell.imageView.image=greenOn;
mySwitch.on=YES;
}else{
cell.imageView.image=greenOff;
mySwitch.on=NO;
}
cell.backgroundView =[[UIImageView alloc] init];
cell.selectedBackgroundView =[[UIImageView alloc] init];
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [self.tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row==1) {
CALayer *mainLayer=cell.contentView.layer;
CALayer *greyOut=[CALayer layer];
CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
greyOut.frame=CGRectMake(0, mainLayer.frame.origin.y, frame.size.width, frame.size.height);
CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor;
CGColorRef clearColor = [UIColor clearColor].CGColor;
greyOut.backgroundColor=darkColor;
[mainLayer addSublayer:greyOut];
if ([[[self.plugStates objectAtIndex:1] substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"0"]){
[greyOut setHidden:NO];
cell.accessoryView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"disconnect70.png"]];
NSLog(@"greyString is 0");
}else{
NSLog(@"greyString is 1");
NSLog(@"superlayer is %@",greyOut.superlayer);
greyOut.backgroundColor=clearColor;
[greyOut setHidden:YES];
//[greyOut removeFromSuperlayer];
}
}
cell.textLabel.text = deviceName;
return cell;
}