私は接続されていても接続されていないデバイスのテーブルビューを持っています。デバイスが接続されている場合、デバイスを制御するオン/オフスイッチ(アクセサリビュー)があります。切断されている場合、スイッチは切断記号に置き換えられます。ここまでは順調ですね。今、私はそれが切断されている場合は、灰色のセルを灰色にするレイヤーを追加したいと明らかに接続されている場合は、レイヤーが消えてしまいます。 (テストのために、私はちょうど状態をトグルするためにボタンに配線されています。私は、レイヤーを配置する方法を考え出したが、私はそれを削除することはできません。ブロックが呼び出されるのは、クリックするたびにlog文が表示されるからです:greyStringは1 "か、greyStringは0です。 私は[layer setHidden:YES]、[層removeFromSuperlayer]とlayer.backroughColr = clearColor、全く効果にUITableViewCellでレイヤーを追加したり削除したりする
- (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;
}
私はこれを試しましたが、全く同じ結果を得ました!私はそれがこのビューであることを確認するために別の色に変更しました。 2回目のボタンを押すたびに別のレイヤーを追加するようなものです。 – mflac