滚动时出现奇怪的错误
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MessageCell", forIndexPath: indexPath) as! ChatTableNewViewCell
let msgtype=CoreDataManager.read(EntityNames.ChatEntity, attributeName: "is_mobile", index: indexPath.row)
if msgtype == "Y" {
cell.lblRecivedMsg.hidden=true
cell.lblSendMsg.text=CoreDataManager.read(EntityNames.ChatEntity, attributeName: "message", index: indexPath.row)
}
else{
cell.lblSendMsg.hidden=true
cell.lblRecivedMsg.text=CoreDataManager.read(EntityNames.ChatEntity, attributeName: "message", index: indexPath.row)
}
cell.lblRecivedMsg.layer.borderColor = UIColor.blackColor().CGColor
cell.lblRecivedMsg.layer.borderWidth = 1
cell.lblRecivedMsg.layer.masksToBounds = false
cell.lblRecivedMsg.layer.cornerRadius = 8
cell.lblRecivedMsg.clipsToBounds = true
cell.lblSendMsg.layer.borderColor = UIColor.blackColor().CGColor
cell.lblSendMsg.layer.borderWidth = 1
cell.lblSendMsg.layer.masksToBounds = false
cell.lblSendMsg.layer.cornerRadius = 8
cell.lblSendMsg.clipsToBounds = true
// Configure the cell...
cell.backgroundColor = UIColor.clearColor()
println(indexPath.row)
return cell
}
这在模拟器和 iPad 中都会发生,而且它并不总是只在我们快速滚动或滚动几次时发生。这真的很烦人:(。请帮忙
最佳答案
从您的代码看来您没有正确重置单元格。
当您设置文本时,您应该为该 TextView 将 hidden 设置为 false。
由于单元格被重复使用,您可以获得一个之前设置为隐藏的单元格。这就是它有时会丢失的原因。
if msgtype == "Y" {
cell.lblRecivedMsg.hidden=true
cell.lblSendMsg.hidden=false
cell.lblSendMsg.text=CoreDataManager.read(EntityNames.ChatEntity, attributeName: "message", index: indexPath.row)
}
else{
cell.lblSendMsg.hidden=true
cell.lblRecivedMsg.hidden=false
cell.lblRecivedMsg.text=CoreDataManager.read(EntityNames.ChatEntity, attributeName: "message", index: indexPath.row)
}
关于ios - 在 UITableView 中滚动时标签文本丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546627/