简体   繁体   中英

adding and removing layer over UITableViewCell

i have a tableview of devices that may or not be connected. if a device is connected, there is an on/off switch (in accessory view) to control it. if it is disconnected, the switched is replaced by a disconnect symbol. so far,so good. Now, i want to add a layer to grey out the cell if it is disconnected, and obviously have the layer disappear if it is connected. (for testing, I just have this wired to a button to toggle state. I have figured out how to place the layer, but i can't remove it. click one-displays the layer, click two-does nothing, click three-makes layer even darker, etc. until it is completely black. the block is being called because with each click, i see the log statement:greyString is 1" or greyString is 0. i have tried [layer setHidden:YES],[layer removeFromSuperlayer] and layer.backroughColr=clearColor, to no effect

- (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;

}

I have had a similar issue with a UICollectionViewCell that should be the same of using tableviews. In my first approach I've coded similar than you've done and nothing happened.

I have solved the issue just creating a tagged UIView that acted as a container of the layer I want to remove. It let me to iterate over all the subviews contained in the UICollectionview (your TableView) and remove the UIView, that contains the layer I want to remove, by using the tag.

The code I've used is:

//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];

I had a similar problem on a custom control on my view, the the layer kept being added. What I had to do was to remove the layer, [greyOut removeFromSuperlayer]; like you had on your code and then call setNeedsLayout method of the view where the layer was added. In your case it would be [cell.contentView setNeedsLayout]

I have an app that darkens the cells based on different situation, but all I did was put an empty UIView over the whole cell and set the background color to black and the alpha to 0.6 for the grayed-out state and 0.0 for clear. Works like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM