Ios UIView未舍入,UITableView单元格回收
我在UITableView的单元格中有一个UIView。我的cellForRowAtIndexPath环绕了该视图。当我打开viewcontroller并看到表格时,所有可见视图都是四舍五入的。但是,当我向下滚动时,显示的第一个新单元格没有圆形视图。我怎么修理它 我的cellforrowatinex如下所示:Ios UIView未舍入,UITableView单元格回收,ios,swift,uitableview,recycle,Ios,Swift,Uitableview,Recycle,我在UITableView的单元格中有一个UIView。我的cellForRowAtIndexPath环绕了该视图。当我打开viewcontroller并看到表格时,所有可见视图都是四舍五入的。但是,当我向下滚动时,显示的第一个新单元格没有圆形视图。我怎么修理它 我的cellforrowatinex如下所示: let cell = tableView.dequeueReusableCell(withIdentifier: "RepeatingCell", for: indexPath) as!
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
}
然而,这并没有解决问题。这是多么不一致,令人沮丧。任何帮助都将不胜感激
邓肯要求的完整代码:
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()
}
func buildCell\u RepeatableCell(indepath:indepath)->CustomTableViewCell{
将cell=tableView.dequeueReusableCell(标识符为:“RepeatingCell”,for:indexath)设为!CustomTableViewCell
cell.delegate=self
tableToSV_左。常数=10
tableToSV_Right.常数=10
tableView.contentInset=UIEdgeInsetsMake(10,0,0,0)
if(indepath作为nsindepath).row
不要在单元格中设置视图的图层属性,而是在CustomTableViewCell
类中设置它们。您可以在awakeFromNib
方法中设置它们
简而言之,将下面的代码移动到awakeFromNib
let circleView = cell.viewToRound
circleView?.layer.cornerRadius = (circleView?.frame.size.width)!/2
circleView?.layer.masksToBounds = true
sudo代码可以是这样的:
awakeFromNib(){
self.viewToRound?.layer.cornerRadius = (viewToRound?.frame.size.width)!/2
viewToRound?.layer.masksToBounds = true
}
不要在cellforrowatinexpath
中设置视图的图层属性,而是在CustomTableViewCell
类中设置它们。您可以在awakeFromNib
方法中设置它们
简而言之,将下面的代码移动到awakeFromNib
let circleView = cell.viewToRound
circleView?.layer.cornerRadius = (circleView?.frame.size.width)!/2
circleView?.layer.masksToBounds = true
sudo代码可以是这样的:
awakeFromNib(){
self.viewToRound?.layer.cornerRadius = (viewToRound?.frame.size.width)!/2
viewToRound?.layer.masksToBounds = true
}
试试这个,它会有用的
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
}
试试这个,它会有用的
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
}
@阿德尔已经在他的评论中告诉了你这个问题和解决方案
在任何情况下,您都需要完全配置单元格。记住,当你得到一个回收的电池时,它可能处于任何可能的剩余状态
在您的情况下,关键点是:
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
}
@VishalSonawane的答案是好的,但要做到这一点,您应该使用两个不同的标识符,一个用于除最后一个之外的所有单元,另一个用于最后一个单元,并使用一个不同的标识符,其中一个单元预配置了mainView hidden。这样,您就不会得到回收的最后一个单元格,并尝试将其用于表视图中的其他位置。@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
}
@VishalSonawane的答案是好的,但要做到这一点,您应该使用两个不同的标识符,一个用于除最后一个之外的所有单元,另一个用于最后一个单元,并使用一个不同的标识符,其中一个单元预配置了mainView hidden。那边哟