2014-03-03 22 views
2

我正在尝试使我的子视图变得前卫,因为删除按钮不断消失。无法将删除子视图带到前面

@implementation MyCell : UITableViewCell 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    for (UIView *subview in self.subviews) { 

     for (UIView *subview2 in subview.subviews) { 

      NSLog(@"HERE!"); 
      if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { 
       NSLog(@"got inside the if!"); 
       [subview2 sendSubviewToBack:subview]; 
       [subview bringSubviewToFront:subview2]; 
       [self.superview bringSubviewToFront:subview2]; 

      } 
     } 
    } 
} 

@end 

我分类了UITableViewCell。两个NSLogs都在打印,所以我知道它已经进入,但我已经尝试了所有这些方法中的三种将视图放在前面。任何想法,为什么这不会工作?

+0

您需要将所有自定义单元格的自定义视图放在单元格的“self.contentView”中,而不是直接放在“self”中。 – rmaddy

+0

像这样'[self.contentview sendSubviewToBack:subview];' '[self.contentview bringSubviewToFront:subview2];' '[self.contentview bringSubviewToFront:subview2];'而不是? – Inbl

+0

不。当你添加子视图时,将它们添加到'contentView'。那么这些代码都不需要。 – rmaddy

回答

0

subview2subview的子视图,它是self的子视图。 sendSubviewToBack:bringSubviewToFront:消息的接收者必须是作为参数给出的视图的超级视图。因此,只有第二次打电话是有效的。正确接听的话会有

[self.superview bringSubviewToFront:self]; 
[self bringSubviewToFront:subview]; 
[subview bringSubviewToFront:subview2]; 

(与同为sendSubviewToBack:。)

至于你的实际问题,我无法弄清楚你正在使用从代码发布视图层次,但似乎这是不正确的。您可能不应将自定义视图设为self的直接子视图,而应将其视为self.contentView(根据Apple's documentation)的子视图。

+0

我试了两种方法,删除按钮仍然消失后,我点击减号按钮。 – Inbl

+0

我的视图层次结构非常简单,只是一个UITableView填充了从UITableViewCell继承的MyCells。 – Inbl

+0

@Inbl请参阅有关'contentView'的编辑。 – Arkku