2013-10-08 70 views
3

我一直有同样的问题,其他人与UITableViewCell剪裁到它的界限。正如其他人正确指出的那样,该单元的contentView有一个新的超级视图。所以我这样做:iOS 7 UITableViewCell剪辑TEMPORARILY

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"HomeTableViewCell"; 
    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(!cell) 
    { 
     cell = [[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     //add views here as subviews to cell.contentView. Set them tags and retrieve later. 
    } 

    cell.clipsToBounds = FALSE; 
    cell.layer.masksToBounds = FALSE; 
    cell.contentView.clipsToBounds = FALSE; 
    cell.contentView.layer.masksToBounds = FALSE; 
    cell.contentView.superview.clipsToBounds = FALSE; 
    cell.contentView.superview.layer.masksToBounds = FALSE; 

    [self loadCell:cell inTable:tableView atIndexPath:indexPath]; 

    return cell; 
} 

loadCell: inTable: atIndexPath:通过检索标记细胞的子视图,并加载适当的内容。这工作正常。没有其他提到clipToBoundslayer.masksToBounds。但是:当我重新加载选中的单元格时,它临时剪辑到边界,然后它碰到上面的代码,并得到它的权利。所以大约1-2秒钟后,我将细胞剪掉。这是ItemCell内部正在发生的事情。我只是让这个类去看看那个clipToBounds属性为TRUE。

@implementation ItemCell 

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
    { 
     [self addObserver:self forKeyPath:@"layer.masksToBounds" options:NSKeyValueObservingOptionNew context:nil]; 
     [self addObserver:self forKeyPath:@"clipsToBounds"   options:NSKeyValueObservingOptionNew context:nil]; 
    } 

    return self; 
} 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    NSNumber *nr = [change objectForKey:@"new"]; 
    if(nr.intValue) 
    { 
     self.clipsToBounds = FALSE;  // STAY FALSE!! 
     self.layer.masksToBounds = FALSE; 
    } 
} 

即使我将它们变为假时他们变为TRUE,我仍然得到剪切/未剪切之间的差距。它只是更小。这在iOS6上没有发生。这是堆栈跟踪时clipsToBounds财产变为1,则可以看到CALayer setMasksToBounds被自动调用:

enter image description here

没有人有任何修补程序的任何想法?谢谢!

+0

我想我有类似的问题。 – djskinner

+0

如果您找到任何解决方案,请让我知道。到目前为止我没有得到任何东西,我有很多项目需要开展工作,而且小故障被经理声明为“可接受的”,所以我再也没有做过这方面的工作。但我仍然好奇。 – George

回答

3

我似乎已经在我的代码中解决了这个问题。首先确保一切都建立在Interface Builder相对于剪裁等,然后全部或部分下面的代码的组合似乎是必要的:

tableView:cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    //ios7 hacks 
    [cell.contentView.superview setClipsToBounds:NO]; 
    [cell.contentView setClipsToBounds:NO]; 
    [cell setClipsToBounds:NO]; 
    return cell; 
} 

tableView:willDisplayCell:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //ios 7 cell background fix 
    [cell.contentView.superview setClipsToBounds:NO]; 
    [cell.contentView setClipsToBounds:NO]; 
    [cell setClipsToBounds:NO]; 
    [cell setBackgroundColor:[UIColor clearColor]]; 
    //ios 7 content clipping fix 
    [cell.contentView.superview setClipsToBounds:NO]; 
    [cell.contentView setClipsToBounds:NO]; 
    [cell setClipsToBounds:NO]; 
} 
+0

谢谢你的回应。我试着用'willDisplayCell'真的有我的希望,但不幸的是它也没有工作。 – George

+0

另一个可能适用的事情是我最终关闭了动画 - 只使用'reloadData'而不是'beginUpdates'和'endUpdates'。显然,这不是理想的解决方案! – djskinner

+0

我正在重新加载2个单元格。先前选择的一个和新选择的一个。开始和结束更新用于当您想要执行更多操作并同时进行动画处理时......如删除一行,更新另一个并插入另一个,与动画每个事物相比。我会尝试使用'UITableViewCellAnimationNone',但AFAIK还是有一些动画参与其中(在WWDC关于表格的视频中看到了这一点)。谢谢! – George