在UITableViewCell上添加和删除图层

时间:2012-01-22 01:43:29

标签: iphone ios uitableview

我有一个可以连接或不连接的设备的表格视图。如果连接了设备,则有一个开/关开关(在附件视图中)来控制它。如果断开连接,则用断开符号代替交换机。到现在为止还挺好。现在,我想添加一个图层,如果它被断开连接,灰显了单元格,显然如果它连接,图层就会消失。 (为了测试,我只是把它连接到一个按钮来切换状态。我已经找到了如何放置图层,但我无法删除它。点击一个 - 显示图层,点击两个 - 什么都不做,点击三层使层更暗,等等,直到它完全变黑为止。正在调用块,因为每次单击,我都会看到log语句:greyString是1"或者greyString是0。 我试过[layer setHidden:YES],[layer removeFromSuperlayer]和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;

}

3 个答案:

答案 0 :(得分:0)

我有一个应用程序,根据不同的情况使单元格变暗,但我所做的只是在整个单元格上放置一个空的UIView并将背景颜色设置为黑色,将灰色的状态设置为0.6,将0.0设置为明确。像魅力一样。

答案 1 :(得分: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];

答案 2 :(得分:0)

我在视图上的自定义控件上遇到了类似的问题,该图层一直在添加。我要做的是删除图层,[greyOut removeFromSuperlayer]; 就像你在你的代码上,然后调用添加图层的视图的setNeedsLayout方法。在你的情况下,它将是[cell.contentView setNeedsLayout]

相关问题