2017-02-22 14 views
1

UIViewUITableViewのセル内にあります。マイcellForRowAtIndexPathラウンドそのビュー。私がviewcontrollerを開いてテーブルを見ると、可視のビューはすべて丸められます。私は下にスクロールするときしかし、最初に表示される新しいセルは、丸みを帯びた見解を持っていません。どうすれば修正できますか?UIViewが丸められていない、UITableViewセルリサイクル

マイcellForRowAtIndexは次のようになります。私はUITableViewsリサイクル細胞と私はおそらく私の問題を引き起こしていると考えていることを理解

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 
} 

。私はそれがビューを隠す最後のセルに関連していると思う。 20個のセルのリストで

は、時には非丸みを帯びたビューは、時には第二、画面上にスクロールする最初として現れます。私が上/下にスクロールすると、ランダムにビューは丸められません!私は理由/パターンがあることを知っていますが、私は何がわかりません。

私もwillDisplayCellに丸めコードを追加してみました:

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

をしかし、それを解決しませんでした。どのようにこれが矛盾するのは非常にイライラしています。どんな助けもありがとう。

DUNCANによって要求されたFULL CODE:代わりにcellForRowAtIndexPathにビューのレイヤーのプロパティを設定するので

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

、あなたのCustomTableViewCellクラスでそれらを設定します。あなたはawakeFromNib方法でそれらを設定することができます。要するに

awakeFromNibにあなたの次のコードを移動します。

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

} 
+0

私はこの考えに興奮しましたが、問題を解決しませんでした。提案していただきありがとうございます。 –

+0

このアプローチの後にエラーはありますか?ソリューションの実装方法を教えてください。 awakeFromNib()FUNC –

+0

オーバーライド{ super.awakeFromNib() self.mainCircle?.layer.cornerRadius =(mainCircle?.frame.size.width)!/ 2 mainCircle?.layer.masksToBounds =真 } –

0

これを試してみて、それが動作します:

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

sudoのコードは次のようなものである可能性があります。

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

Nope。私は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が非表示になることはありません。VishalSonawaneの答え@

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 
} 

は良いですが、あなたは、MAINVIEW隠されて事前に構成されたセルで、2つの異なる識別子、すべてのセルに1つが、最後の1、および異なる識別子を使用する必要があり、その仕事をするために最後のセル。そうすれば、リサイクルされた最後のセルは取得されず、テーブルビューの他のポジションの1つに使用されます。

+0

こんにちはダンカン、私はあなたの時間を無駄にしてしまって申し訳ありませんが、私のSOの投稿のテキスト割り当てコードは、私は間違いなくその行のunhideを削除しました。間違いなく隠されています。 mainViewが表示されていない場合は、丸められていないビューは一切表示されません –

+0

私たちは見ることができないコードをデバッグすることはできません。おそらく 'cellForRow(at:)'メソッド全体を表示するべきです。 –

+0

申し訳ありませんが、ちょっと時間がかかるので、みんなを圧倒しないようにしようとしましたが、ちょうど追加しました! –

関連する問題