2017-02-22 62 views
1

我有一个UIView在一个UITableView的单元格内。我的cellForRowAtIndexPath四舍五入该视图。当我打开视图控制器并查看表格时,所有可见视图都被舍入。但是,当我向下滚动时,出现的第一个新单元格不具有圆角视图。我如何解决它?UIView不圆形,UITableView细胞回收

cellForRowAtIndex看起来是这样的:

let cell = tableView.dequeueReusableCell(withIdentifier: "RepeatingCell", for: indexPath) as! CustomTableViewCell 
cell.delegate = self 
tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0) 

if (indexPath as NSIndexPath).row < tableListInfo_Context.count { 
    let task = tableListInfo_Context[(indexPath as NSIndexPath).row     
    let accState = task.value(forKey: "state") as? String 
    //Removed assigning labels their text 
    let mainView = cell.mainView 
    mainView.isHidden = false 
    let circleView = cell.viewToRound 
    circleView?.isHidden = false 
    circleView?.layer.cornerRadius = (circleView?.frame.size.width)!/2 
    circleView?.layer.masksToBounds = true   
    cell.layoutMargins = UIEdgeInsetsMake(0, 1000, 0, 1000) 
} else { 
    //My last cell hides several views to reveal a view under them 
    //If I comment this one line out, I get the not-rounded problem only once and it does not show up again even if I continue scrolling up/down 
    let mainView = cell.mainView 
    mainView.isHidden = true 
    let circleView = cell.viewToRound 
    circleView?.isHidden = true 
} 

我明白UITableViews回收细胞,我认为这很可能导致我的问题。我认为这与我隐藏视图的最后一个单元格有关。

在20个单元格的列表中,有时非圆角视图显示为第一个在屏幕上滚动,有时是第二个。随着我向上/向下滚动,随机地观看视频不会变圆!我知道有一个原因/模式,但我不知道是什么。

我也尝试添加舍入代码willDisplayCell

let circleView = myCell.mainCircle 
circleView?.layer.cornerRadius = (circleView?.frame.size.width)!/2 
circleView?.layer.masksToBounds = true 

然而,这并没有解决这个问题。这是多么不协调令人非常沮丧。任何帮助,将不胜感激。

全码的要求邓肯:

func buildCell_RepeatableCell (indexPath: IndexPath) -> CustomTableViewCell   { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "RepeatingCell",  for: indexPath) as! CustomTableViewCell 
    cell.delegate = self 

    tableToSV_Left.constant = 10 
    tableToSV_Right.constant = 10 
    tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0) 

    if (indexPath as NSIndexPath).row < curLifeList_Context.count { 
     resetDataCell_ToDefaultLaylout(cell: cell, hideExpand: true) 
     let task = curLifeList_Context[(indexPath as NSIndexPath).row] 
     let accState = task.value(forKey: "state") as? String 
     cell.nameLabel!.text = task.value(forKey: "name") as? String 
     cell.descLabel!.text = task.value(forKey: "desc") as? String 
     let redTrashIcon = UIImage(named: "trash") 
     let maskedIcon = redTrashIcon?.maskWithColor(color: .red) 
     cell.row5_LeftBtnImg?.image = maskedIcon 
     let doneGreen = UIColor(colorLiteralRed: 83/255, green: 223/255, blue: 56/225, alpha: 0.9) 
     let greenCheck = UIImage(named: "check_Green") 
     let maskedCheck = greenCheck?.maskWithColor(color: doneGreen) 
     cell.row5_RightBtnImg?.image = maskedCheck 
     roundQtyCircle(view: cell.mainCircle) 
     if accState == "Selected" { 
      cell.circleLine.backgroundColor = doneGreen 
      cell.mainCircle.borderColor = doneGreen 
      cell.hdrCrossOutLine.isHidden = false 
      cell.row5_RightBtnImg.alpha = 0.5 
     } else { 
      cell.circleLine.backgroundColor = UIColor(colorLiteralRed: 211/255, green: 211/255, blue: 211/255, alpha: 0.9) 
      cell.mainCircle.borderColor = UIColor(colorLiteralRed: 211/255, green: 211/255, blue: 211/255, alpha: 0.9) 
      cell.hdrCrossOutLine.isHidden = true 
      cell.row5_RightBtnImg.alpha = 1.0 
     } 
     cell.dataHdr_Left!.text = doneColon_lczd 
     cell.dataHdr_Right!.text = lastDoneColon_lczd 
     cell.dataVal_Left!.text = "0" 
     cell.dataVal_Right!.text = "-" 
     if let lastCompDate = task.value(forKey: "lastCompleted") as? Date { 
      cell.dataVal_Left!.text = "\(task.value(forKey: "timesCompleted") as! Int)" 
      if NSCalendar.current.isDateInToday(lastCompDate) { 
       cell.dataVal_Right!.text = "Today" 
       cell.dataBotDtl_Right.text = "" 
      } else { 
       let secondsUntilChange = (lastCompDate.seconds(from: now)) * -1 
       print("Seconds ago \(secondsUntilChange)") 
       var timeAgo = getDateString(secondsUntilChange, resetDate: lastCompDate) 
       timeAgo.remove(at: timeAgo.startIndex) 
       cell.dataVal_Right!.text = timeAgo 
      } 
     } 
     cell.layoutMargins = UIEdgeInsetsMake(0, 1000, 0, 1000) 
    } else { 
     buildAddNew_ForRepeatableCell(cell: cell) 
    } 

    return cell 
    } 

    func resetDataCell_ToDefaultLaylout (cell: CustomTableViewCell, hideExpand: Bool) { 
    cell.addnewBtn.isHidden = true 
    cell.nameLabel.isHidden = false 
    cell.dataHdr_Left.isHidden = false 
    cell.dataHdr_Right.isHidden = false 
    cell.dataTopDtl_Right.isHidden = false 
    cell.dataBotDtl_Right.isHidden = false 
    cell.mainBox.isHidden = false 
} 

func buildAddNew_ForRepeatableCell (cell: CustomTableViewCell) { 
    cell.addnewBtn.isHidden = false 
    cell.descLabel.text = addNew_lczd //For editor 
    cell.mainBox.isHidden = true 
    cell.layoutMargins = UIEdgeInsetsMake(0, 1000, 0, 1000) 
    cell.container.layoutIfNeeded() 
} 
+0

在'cellForRow'方法的其他块中,你隐藏'mainView',但是你并没有在if块中隐藏它。 – Adeel

+0

@Adeel在删除文本分配代码时只是一个错误,我不小心删除了那个隐藏线。如果mainView没有被隐藏,我根本看不到非圆形的视图。 –

回答

0

而是在cellForRowAtIndexPath设置视图的图层属性的,在你的CustomTableViewCell类设置。您可以在awakeFromNib方法中设置它们。

总之把你的下面的代码移到awakeFromNib

let circleView = cell.viewToRound 
    circleView?.layer.cornerRadius = (circleView?.frame.size.width)!/2 
    circleView?.layer.masksToBounds = true 

须藤代码可能是这样的:

awakeFromNib(){ 
self.viewToRound?.layer.cornerRadius = (viewToRound?.frame.size.width)!/2 
viewToRound?.layer.masksToBounds = true 

} 
+0

我很高兴这个想法,但它没有解决问题。感谢您的建议。 –

+0

此方法之后是否有任何错误?你能证明你是如何实施我的解决方案的? –

+0

倍率FUNC awakeFromNib(){ super.awakeFromNib() self.mainCircle?.layer.cornerRadius =(mainCircle?.frame.size.width)!/ 2 mainCircle?.layer.masksToBounds =真 } –

0

试试这个,它会工作。

override func awakeFromNib() { 
     super.awakeFromNib() 
     self.viewToRound?.layer.cornerRadius = (viewToRound?.frame.size.width)!/2 
     viewToRound?.layer.masksToBounds = true 
    } 


override func prepareForReuse() { 
     super.prepareForReuse() 
     self.viewToRound?.layer.cornerRadius = (viewToRound?.frame.size.width)!/2 
     viewToRound?.layer.masksToBounds = true 
    } 
+0

没有。我不知道prepareForReuse存在。我非常兴奋,但这也行不通。当我快速而缓慢地滚动时,90%的时间都是圆形的,但仍然是非圆形的。 –

+0

你可以分享我你的源代码(示例项目)将更新和恢复你的解决方案,在我的回答中使用相同的方法 – Krunal

0

@Adeel已经告诉过你他的评论中的问题和解决方案。

你总是需要在每种情况下完全配置你的单元格。请记住,当您获得再生细胞时,它可能处于任何可能的剩余状态。

在你的情况,关键位:

if (indexPath as NSIndexPath).row < tableListInfo_Context.count { 
    //Configure cells that are not the last cell 
} else { 
    //Configure cells the last cell 
    mainView.isHidden = true 
} 

你隐藏在您正在配置的最后一个单元格的情况下mainView,但不取消隐藏它,如果它不是最后一个单元格。因此,如果您配置最后一个单元格,请将其滚动到屏幕外,然后向屏幕顶部滚动,再循环单元格将被重用,而不是最后一个单元格的indexPath,并且它的mainView将永远不会被隐藏。

if (indexPath as NSIndexPath).row < tableListInfo_Context.count { 
    //Configure cells that are not the last cell 
    //----------------------------- 
    // This is what you are missing 
    mainView.isHidden = false 
    //----------------------------- 
} else { 
    //Configure cells the last cell 
    mainView.isHidden = true 
} 

@ VishalSonawane的回答是好,但要作出这样的工作,你应该使用2个不同的标识符,一个对所有细胞,但最后一个,和不同的标识符,用细胞预先配置MAINVIEW隐藏,为最后一个单元格。这样你不会得到一个循环的最后一个单元格,并尝试将它用于表格视图中的其他位置之一。

+0

嗨邓肯,我很抱歉浪费你的时间,但这只是我的删除我的SO帖子的文本分配代码,我不小心删除了那个取消隐藏行。它绝对不被隐藏。如果mainView没有被隐藏,我根本看不到非圆形的视图 –

+0

我们不能调试我们看不到的代码。你可能应该显示整个'cellForRow(at:)'方法。 –

+0

好吧,它有点冗长,所以我试图不压倒每个人,但只是添加它! –