在UITableView

时间:2015-05-30 13:12:20

标签: 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中都会发生这种情况,而且只有当我们滚动得非常快或滚动几次时它才会发生。这真烦人:(。请帮忙

1 个答案:

答案 0 :(得分:2)

从您的代码中看起来您没有正确重置单元格。

设置文本时,您应该为该文本视图将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)
}