简体   繁体   English

reloadSection后,UITableView中的阴影仍然存在:

[英]Shadows Persist in UITableView after reloadSections:

Due to our designer being a sadist, I have a UITableView with a segmented control that switches between two different types of cells in separate feeds. 由于我们的设计师是虐待狂,所以我有一个带有分段控件的UITableView,该控件可在单独的供稿中在两种不同类型的单元格之间进行切换。 The cells are dequeued with different identifiers and classes — this all works fine. 单元使用不同的标识符和类出队-一切正常。 The cells share a parent but are different sizes and for optimization reasons I set the layer.shadowpath manually in layoutSubviews() of the parent. 这些单元共享一个父级,但是大小不同,出于优化原因,我在父级的layoutSubviews()中手动设置了layer.shadowpath I need the shadows: designer's wishes. 我需要阴影:设计师的愿望。

The issue is that after I switch to the second segment, some of the way down the table there are shadows dangling from what I believe are the cells above. 问题是,当我切换到第二部分后,在桌子下方的某些地方,我认为上方的单元格中悬挂着阴影。 As you can see from the first image, there are two shadows, and if I scroll down to occlude the top-most visible cell the shadow disappears, which leads me to the believe that the shadows are offset. 从第一个图像中可以看到,有两个阴影,如果我向下滚动以遮挡最顶部的可见单元,阴影就会消失,这使我相信阴影是偏移的。 Further scrolling makes these shadows disappear and not reappear again until switching tabs again. 进一步滚动使这些阴影消失,直到再次切换选项卡时才再次出现。 The rest of the shadows are fine. 其余的阴影很好。

two shadows 两个影子

两个影子

scroll down slightly 向下滚动

一个影子消失了

When switching back to the previous tab, where the cells are taller, there are also shadow issues, but those shadows are too short. 当切换回上一个较高单元格的选项卡时,也存在阴影问题,但是这些阴影太短了。 As noted, the code that sets the shadow path is in the parent class, and the parent class is responsible for making and laying-out the top-most "card" view that contains the custom subCells. 如前所述,设置阴影路径的代码在父类中,并且父类负责制作和布局包含自定义subCell的最顶层“卡片”视图。

I do everything programmatically: setting up the views and the Autolayout. 我以编程方式进行所有操作:设置视图和自动版式。 Currently cell heights are hard-coded. 当前,像元高度是硬编码的。 I'm not sure what information is relevant as I am completely at a loss, so here is how I set the shadowPath . 我不确定什么信息是相关的,因为我完全不知所措,所以这是我设置shadowPath

override func layoutSubviews() {
    super.layoutSubviews()
    cardView.layer.shadowPath = UIBezierPath(rect: cardView.bounds).CGPath
}

For simplicity the card is layout out in the contentView with the following visual format: 为简单起见,该卡以下列可视格式在contentView进行布局:

"V:|-marginV-[card]-marginV-|"
"H:|-marginH-[card]-marginH-|"

For whatever reason, even though I was using separate classes and separate reuseIdentifier s, the first reused cells just out of the view port were still sized as the tall cells in the other segment. 无论出于何种原因,即使我使用的是单独的类和单独的reuseIdentifier ,在视reuseIdentifier的第一个重用单元的大小仍与其他段中的高单元格相同。 When I changed 当我改变

let cell = tableView.dequeueReusableCellWithIdentifier(booksReuseIdentifier) as! ProfileBookCell let cell = tableView.dequeueReusableCellWithIdentifier(booksReuseIdentifier) as! ProfileBookCell to include the indexPath as let cell = tableView.dequeueReusableCellWithIdentifier(booksReuseIdentifier) as! ProfileBookCell包括indexPath作为

let cell = tableView.dequeueReusableCellWithIdentifier(booksReuseIdentifier, forIndexPath: indexPath) as! ProfileBookCell let cell = tableView.dequeueReusableCellWithIdentifier(booksReuseIdentifier, forIndexPath: indexPath) as! ProfileBookCell the recycling issue was remedied and bounds were properly computed. let cell = tableView.dequeueReusableCellWithIdentifier(booksReuseIdentifier, forIndexPath: indexPath) as! ProfileBookCell的回收问题已得到纠正,边界得到了正确的计算。 I tried layoutIfNeeded in a dozen different places to no effect, but this fixed it. 我在十几个不同的地方尝试了layoutIfNeeded ,但是没有解决,但这已经解决了。

I had the same exact problem and I tried the current marked solution but that, nor anything else seemed to work. 我有相同的确切问题,我尝试了当前的标记解决方案,但是那似乎没有任何其他效果。 After trying so many other things I finally tried moving my add shadow code inside the layoutSubviews function of my subclassed UITableViewCell and it finally worked! 在尝试了许多其他事情之后,我终于尝试将添加的阴影代码移到我的子类UITableViewCell的layoutSubviews函数中,终于成功了! I think this worked because the cell's final size isn't always calculated properly until the layouSubviews call and it needs the proper size to draw the shadow. 我认为这行得通,是因为在layouSubviews调用之前,单元的最终大小始终无法正确计算,并且需要适当的大小才能绘制阴影。

override func layoutSubviews() {
     super.layoutSubviews()
    addShadow(cell: self)
}

private func addShadow(cell:UITableViewCell) {
    cell.layer.shadowOffset = CGSize(width:1, height:1)
    cell.layer.shadowColor = UIColor.black.cgColor
    cell.layer.shadowRadius = 1
    cell.layer.shadowOpacity = 0.6

    cell.clipsToBounds = false

    let shadowFrame: CGRect = (cell.layer.bounds)
    let shadowPath: CGPath = UIBezierPath(rect: shadowFrame).cgPath
    cell.layer.shadowPath = shadowPath
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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