TableViewCell overlaps text

2.9k views Asked by At

I'm having a problem with my TableViewCell

I have two type of cell in my storyboard. when i scroll, the text overlaps in some cells. I Try everything but I do not know how else to do. thank you very much for the help

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        var storeNew = systemsBlog.getStore(listNews[indexPath.row].getIdStore())
        var newNotice = listNews[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell

                cell!.nameLabel.text = storeNew.getName()
                cell!.postLabel?.text = newNotice.getText()
                cell!.postLabel?.numberOfLines = 0
                cell!.dateLabel.text = newNotice.getDate()
                cell!.typeImageView?.tag = indexPath.row;
                return cell!
}



class TimelineCell : UITableViewCell {

    @IBOutlet var nameLabel : UILabel!
    @IBOutlet var postLabel : UILabel?
    @IBOutlet var dateLabel : UILabel!

    override func awakeFromNib() {
     postLabel?.font = UIFont(name: "Roboto-Thin", size: 14)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }

enter image description here

5

There are 5 answers

0
Carolina On BEST ANSWER

I can fix the problem. In the storyboard, the label have unchacked "Clears Graphics Context". I checked and for now it solved! Thanks for the help!

0
Matteo Piombo On

You can try setting:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 44.0 // Set this value as a good estimation according to your cells
}

In the View Controller containing your tableView.

Make sure the layout constraints in your TimelineCell define a clear line of height constraints

Another option is responding to:

tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
 return 44.0 // Your height depending on the indexPath
}

Always in the ViewController that contains your tableView and, I assume, is the tableView's UITableViewDelegate

Hope this helps

1
Tyler McKay On

I had a similar issue with one of my UITableViews in the past. There are a bunch of things that could cause this, but maybe it is the same thing that happened to me.

I see that you are using a custom tableViewCell. What could be happening is when you set the text of the cell, it adds a label view with that text. Now say you scroll through the tableview and that cell disappears. If you were to reuse that cell and you did not remove the label from the subview, or set the text of that label again to the desired text, you will be reusing the tableviewcell with a previous label on it and adding a new label with new text to it, overlapping the text.

My suggestion would be to make sure you do not keep adding UIlabels as subviews in the TimelineCell class unless no label exists. if a label exists edit the text of that label not of the cell.

0
Icaro On

As per apple documentation:

The table view’s data source implementation of tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.

It seems that your problem is that you not always setting postLabel, causing it to write on top of the other cells, try this:

//reuse postLabel and set to blank it no value is returned by the function    
let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell

            cell!.nameLabel.text = storeNew.getName()
            if newNotice.getText() != nil{
                cell!.postLabel.text = newNotice.getText()
            } else {cell!.postLabel.text = ''}
            cell!.postLabel.numberOfLines = 0
            cell!.dateLabel.text = newNotice.getDate()
            cell!.typeImageView?.tag = indexPath.row;
            return cell!
}

//Make postLabel mandatory and set the font details in the xcode
class TimelineCell : UITableViewCell {
@IBOutlet var nameLabel : UILabel!
@IBOutlet var postLabel : UILabel!
@IBOutlet var dateLabel : UILabel!

override func awakeFromNib() {
 //set this in xcode
}

override func layoutSubviews() {
    super.layoutSubviews()
}

Also be sure that you are not creating any UI element and appending to the cell, as if you are you need to dispose it before you recycle the cell.

0
Prakasha Maravanthe On

Set cell to nil that will fix some error. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

var cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? ImageCellTableViewCell
    cell = nil
    if cell == nil {
        cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath) as? ImageCellTableViewCell
    }
    cell.yourcoustomtextTextLabel.text = "this is text"
    cell.yourcoustomtextImageView.image = image
    return cell
}