2016-11-18 36 views
0

我有一个非常简单的UIScrollView与一些内容(许多子视图)。这个滚动视图用于显示用户创建的一些帖子(图片+文字)。其中一种观点实际上是作者的形象,它溢出了底部细胞的界限。因此它与后面的单元格重叠,并使用clipToBounds = false我可以获得所需的结果。如果我向下滚动,一切都很好。当我开始向前滚动时,以前覆盖的视图会被剪切。dequeueReusableCell休息cliptobounds

Cell overlapping working fine Cell overlapping not working (when I scroll up)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cellIdentifier = (indexPath.row % 2 == 0) ? "FeedCellLeft" : "FeedCellRight"; 
    let cell = feedScrollView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! FeedCell; 
    self.setUpCell(cell, atIndexPath: indexPath); 
    return cell 
} 

setUpCell功能只是执行一些UI相关的任务

let row = indexPath.row 

    cell.postImage.downloadImageFrom(link: rows[row].image, contentMode: .scaleToFill) 
    cell.postAuthorImage.downloadImageFrom(link: "https://pbs.twimg.com/profile_images/691867591154012160/oaq0n2zy.jpg", contentMode: .scaleToFill) 
    cell.postAuthorImage.layer.cornerRadius = 22.0; 
    cell.postAuthorImage.layer.borderColor = UIColor.white.cgColor 
    cell.postAuthorImage.layer.borderWidth = 2.0; 
    cell.postAuthorImage.layer.masksToBounds = true; 

    cell.selectionStyle = .none 

    cell.postData.layer.cornerRadius = 10.0; 

    cell.contentView.superview?.clipsToBounds = false; 
    cell.clipsToBounds = false; 


    if (indexPath.row % 2 != 0) { 
     cell.postData.transform = CGAffineTransform.init(rotationAngle: (4 * .pi)/180); 
    } else { 
     cell.postData.transform = CGAffineTransform.init(rotationAngle: (-4 * .pi)/180); 
    } 

看来,双端队列操作打破我做了(使用自动版式)的布局。我试过很多解决类似这样的

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.contentView.superview?.clipsToBounds = false; 
    cell.clipsToBounds = false; 
    cell.contentView.clipsToBounds = false; 
} 

但结果看起来总是相同的。每行的高度是固定的。

回答

1

我认为问题是与子视图的层次结构。当您向下滚动时,您的细胞从顶部到底部出队,并以相同顺序添加到UITableView,并且所有外观都很好。因为前一个单元格在视图层次结构中高于以下内容。 但是,当您向上滚动时,单元格会从下到上出列,这意味着顶部的单元格位于先前单元格的“后面”。您可以使用Xcode的Debugging View Hierarchies功能轻松检查它。

你可以尝试bringSubviewToFront:例如:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.superview.bringSubview(toFront cell) 
} 

更新版本

我在操场做的小调查,发现只有一个执行重叠合理选择单元没有巨大的性能问题。该解决方案基于cell.layer.zPosition属性并且工作正常(至少在我的Playground中)。我更新的代码内willDisplay cell:与下列之一:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.layer.zPosition = (CGFloat)(tableView.numberOfRows(inSection: 0) - indexPath.row) 
} 

根据用于.zPosition的文件(Apple Developer Documentation):

该属性的缺省值是0改变的这个属性的值更改屏幕上从前到后的图层排序。数值越高,该层视觉上就越靠近观察者而不是具有较低值的层。这可能会影响帧矩形重叠的图层的可见性。

因此,我使用当前dataSource计数器作为被减数当前小区为减数indexPath.row来计算每个小区的层的zPosition

您可以下载完整版我的游乐场here的。

+0

嗨,这是一个伟大的提示,它似乎工作,但相反方向。现在它在我向上滚动时起作用,但是当我向下滚动时不起作用,因为在首先布局单元时,也会调用'willDisplayCell'。 –

+0

@DiegoViganò,检查我的更新答案。 – dive

+0

G R E A T!有效!我欠你一杯啤酒!再次感谢! –