2017-02-22 61 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

好吧,它有點冗長,所以我試圖不壓倒每個人,但只是添加它! –