TableViewCell overlaps text
Asked Answered
J

5

7

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

Jeremie answered 15/6, 2015 at 23:26 Comment(6)
I can see TimelineCell where is the other type?Siloa
This kind of thing can happen when reusing cells and continually adding subviews in the cell for row method. However I can't see any of this happening in the code you have posted. Is there more code?Thrombosis
@lcaro, the other type i don't use because now i try to solve this problem.Jeremie
Beau, I don't have any code to adding subviews to the cell.Jeremie
@Carol, what is happening in the cell? Is the text from 2 different cells? Or is a misalignment in the cell?Siloa
Is the text from 2 different cellsJeremie
J
7

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!

Jeremie answered 16/6, 2015 at 4:13 Comment(0)
M
0

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.

Mcatee answered 16/6, 2015 at 3:22 Comment(1)
Hi, Tyler. I do not add the label in the cell. I have the label on storyboard and only set the text.Jeremie
S
0

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.

Siloa answered 16/6, 2015 at 3:33 Comment(0)
S
0

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

Shana answered 16/6, 2015 at 4:0 Comment(0)
P
0

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
}
Piezoelectricity answered 8/12, 2016 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.