ios - 在 UITableView 中滚动时标签文本丢失

标签 ios uitableview swift

滚动时出现奇怪的错误

enter image description here Label text missing

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/

相关文章:

ios - 子层未按预期隐藏/显示

.net - 设计可移植到 iOS/Monotouch 的 Windows Phone 游戏的技巧

ios - 如何在@IBAction中调用URL数组?

ios - 数据结构和数据模型之间的区别 - 例如?

ios - TableView 和按钮(崩溃)

ios - 重播套件未记录父应用程序中的第二个应用程序

ios - 我的用户默认设置不保存我的 slider 值 - Swift 3

ios - 使用协议(protocol)检测 UISegmentedControl 中的值变化

ios - Magento OAuth 身份验证无法处理自定义 URL 方案

objective-c - UITableView 中心单元格详细信息