Blurry UILabel as programmatic subview of UITableViewCell contentView

后端 未结 9 822
离开以前
离开以前 2021-02-04 02:26

I am adding a UILabel instance as a subview of my custom UITableViewCell instance\'s contentView.

When I select the cell, the row

相关标签:
9条回答
  • 2021-02-04 03:13

    Another cause of garbled/blurry text is cell reuse. If you are de-queuing a reusable cell then it may redraw with different dimensions somewhere else and again be re-used when it gets to your cell with the garbled text.

    To ensure the cells are unique be sure to allocate a new cell for the indicies where the text is garbled, and mark that UITableViewCell instance with a different reuse identifier. This is only practical of course if you're dealing with a very small number of cells and if you know exactly which cells are causing problems.

    0 讨论(0)
  • 2021-02-04 03:13

    Does -setNeedsLayout get called even for dequeued reusable cells? If so, the cell will already have the label added to the content view, and you will draw it twice, making it blurry. You can inefficiently solve this by removing all of the content view's subviews before you add your subview:

    for (UIView *subview in [[self contentView] subviews]) {
         [subview removeFromSuperview];
    }
    

    A better solution would be to provide properties on your cell subclass to let you modify the content of a reused cell as-needed, rather than rebuilding its view hierarchy from scratch.

    0 讨论(0)
  • 2021-02-04 03:14

    In my case, having set shouldRasterize = YES on the CGLayer of the view containing the UILabel was the culprit. Removing that line made the text nice and crisp.

    0 讨论(0)
提交回复
热议问题