问题描述:

I am getting this weird error when scrolling

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

}

网友答案:

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)
}
相关阅读:
Top