Vineeth Vijayan
Vineeth Vijayan

Reputation: 1329

Label text missing when scrolling in UITableView

I am getting this weird error when scrolling

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
}

This happens in both simulator and in iPad, moreover it doesn't happen always only when we scroll very fast or scroll couple of times. This is really annoying :(. Please help

Upvotes: 0

Views: 1347

Answers (1)

Rory McKinnel
Rory McKinnel

Reputation: 8024

From your code it looks like you are not resetting the cell properly.

When you set the text you should set hidden to false for that text view.

Since cells are being reused, you could get one which has been set to hidden previously. That is why it is missing sometimes.

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)
}

Upvotes: 2

Related Questions