2012-01-22 19 views
0

私は接続されていても接続されていないデバイスのテーブルビューを持っています。デバイスが接続されている場合、デバイスを制御するオン/オフスイッチ(アクセサリビュー)があります。切断されている場合、スイッチは切断記号に置き換えられます。ここまでは順調ですね。今、私はそれが切断されている場合は、灰色のセルを灰色にするレイヤーを追加したいと明らかに接続されている場合は、レイヤーが消えてしまいます。 (テストのために、私はちょうど状態をトグルするためにボタンに配線されています。私は、レイヤーを配置する方法を考え出したが、私はそれを削除することはできません。ブロックが呼び出されるのは、クリックするたびに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; 

}

答えて

0

私は別の状況に基づいてセルを暗くするアプリケーションを持っていますが、私がしたのは、全体のセルに空のUIViewを置き、背景色を黒に設定し、グレーアウト状態のアルファ値を0.6に、明らかです。魅力のように動作します。

+0

私はこれを試しましたが、全く同じ結果を得ました!私はそれがこのビューであることを確認するために別の色に変更しました。 2回目のボタンを押すたびに別のレイヤーを追加するようなものです。 – mflac

0

私はUICollectionViewCellで同様の問題がありました。これはtableviewsを使用するのと同じでなければなりません。 私の最初のアプローチでは、これまでと同じようにコーディングしましたが、何も起こりませんでした。

削除したいレイヤーのコンテナとして機能するタグ付きUIViewを作成するだけで問題は解決しました。 UICollectionview(あなたのTableView)に含まれているすべてのサブビューを繰り返し処理し、タグを使用して削除したいレイヤーを含むUIViewを削除しました。

私が使用したコードです:私は私の見解でカスタムコントロールに同様の問題があった

//1.- iterate over all subview in order to delete the view tagged as 98. I do this first of all because I don't know at this point if there are any cell already placed 
for (UIView *subview in [globoChat subviews]) { 
    if (subview.tag == 98) { 
     [subview removeFromSuperview]; 
    } 
} 
//Create the container UIView and then tag it. 
    UIView *nuevaVista = [[UIView alloc]initWithFrame:globoChat.bounds]; 
    nuevaVista.tag=98; 

    //create layer 
    CALayer *globoPorDebajo = [CALayer layer]; 
    globoPorDebajo.frame = CGRectMake(0,0,20,20); 

    //add layer to the uiview that will contain it 
    [nuevaVista.layer addSublayer:globoPorDebajo]; 

    //add the container view to the parent view 
    [globoChat addSubview:nuevaVista]; 
0

、層が追加され続け。私がしなければならなかったのは、レイヤーを削除することでした。[greyOut removeFromSuperlayer]; のようにして、レイヤーが追加されたビューのsetNeedsLayoutメソッドを呼び出します。あなたの場合は[cell.contentView setNeedsLayout]

関連する問題