2017-08-01 81 views
0

我有一个UITableView与3个单元格,最终将作为仪表板。我正在尝试使用KDCircularProgress来配置带有圆形进度视图的顶部单元格 - 我有一个UIView,我用约束定位,然后以编程方式添加循环进度对象。更新方向变化UITableViewCell子视图约束

但是,当我将设备旋转到风景时,进度视图会发生变化(请参阅第一幅图像)。我尝试过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

在cellForRow使用addSubview

enter image description here

回答

0

避免。当单元格被重用时,这个额外添加的视图将不会被删除,并且在重新加载单元格时,视图将被重叠。当重叠尺寸和位置相同时,您不会看到这种影响,但如果您在旋转的情况下,您可以看到它。在XIb中静态添加视图或查看您想要的位置,而不是在cellForRowAt中查看。

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

如果添加子视图中cellForRowAt你会觉得挺举滚动时的tableView。

+0

好的,理解。当你说'在任何地方查看'添加视图时,你有什么想法? – Tom

+0

我想你会将DashboardTopTableViewCell作为Storyboard或XIB中的原型单元格,您可以在其中添加。 –

+0

对不起,应该提到KDCircularProgress没有Storyboard对象,因此我以编程方式创建它。 – Tom

相关问题