在方向更改时更新UITableViewCell子视图约束

时间:2017-08-01 11:18:48

标签: ios swift

我有一个UITableView有3个单元格,最终将用作仪表板。我正在尝试使用KDCircularProgress使用循环进度视图配置顶部单元格 - 我有一个UIView我使用约束进行定位,然后以编程方式添加循环进度对象。

但是,当我将设备旋转到横向时,进度视图会移动(请参见第一张图像)。我尝试了setNeedsLayout()layoutSubviewslayoutIfNeeded()的各种组合,但没有运气。

我还在reloadData()中尝试了willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval),这让我稍微进一步了解了视图的正确调整大小(请参阅第二张图片),但它创建了一个副本。摘自以下cellForRowAt方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    switch indexPath.row {
    case 0:
        let topCell = tableView.dequeueReusableCell(withIdentifier: "topCell", for: indexPath) as! DashboardTopTableViewCell

        //Progress config
        topCell.progressBar = KDCircularProgress(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: topCell.circularProgressContainer.frame.width, height: topCell.circularProgressContainer.frame.height)))
        let colorForProgress: UIColor = UIColor(red: 69.0/255.0, green: 110.0/255.0, blue: 178.0/255.0, alpha: 0.8)
        topCell.progressBar?.progressColors = [colorForProgress]
        let progressAsAngle = ((percentageComplete/100)*360)
        topCell.progressBar?.animate(toAngle: progressAsAngle, duration: 2.0, completion: nil)
        topCell.circularProgressContainer.addSubview(topCell.progressBar!)

        return topCell

所以我有点卡住 - 有什么建议吗?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

避免在cellForRow中使用addSubview。当单元格被重用时,这个额外添加的视图将不会被删除,并且在重新加载单元格时,视图将重叠。当重叠具有相同的大小和位置时,您将看不到影响,但在您旋转的情况下,您可以看到它。在XIb中静态添加视图或在任何地方查看视图,但不在cellForRowAt中。

但是,您可以在cellForRow中更改子视图的属性。

如果你在cellForRowAt中添加子视图,你会在滚动tableView时感觉到这个混蛋。

相关问题