TableViewCell overlaps text

I have a problem with my TableViewCell

I have two types of cells in my storyboard. when I browse, the text overlaps in some cells. I try everything, but I don’t know how to do it. thank you for 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

+6
source share
5 answers

I can solve the problem. In the storyboard, the shortcut is off "Clears the graphics context." I checked and decided at the moment! Thanks for the help!

+5
source

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

I see that you are using your own tableViewCell. What can happen when you set the text of a cell, it adds a label view with that text. Now say that you are viewing the table and this cell disappears. If you reused this cell and you didn’t remove the label from the preview or returned the text of this label to the desired text, you will reuse tableviewcell with the previous label on it and add a new label with new text to it, overlapping the text.

My suggestion was to make sure that you do not add UIlabels as subtasks in the TimelineCell class if there is no label. if a label exists, edit the text of that label is not a cell.

0
source

According to the apple documentation :

The table discusses the implementation of the tableView data source: cellForRowAtIndexPath: always reset all content when the cell is reused.

It seems your problem is that you do not always install postLabel, forcing it to write on top of other cells, try the following:

 //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 make sure that you are not creating a user interface element or adding it to a cell, as if you had to dispose of it before you recycle the cell.

0
source

You can try installing:

 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 layout constraints in TimelineCell define a clear line of height constraints

Another option responds to:

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

Always in the ViewController that your tableView contains, and I suppose this is a tableView UITableViewDelegate

Hope this helps

0
source

Set the cell to zero, which 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 } 
0
source

Source: https://habr.com/ru/post/989167/


All Articles