Swift:UITableViewCell中的像素化文本

时间:2018-08-22 03:21:13

标签: ios swift uitableview uikit uilabel

我正在更改ApplicationController.renderer的颜色和UILabel中可重用原型单元中的文本。对于一个特定的单元格(一个我正在从数据库中获取信息的单元格),文本将变形并像素化。我怀疑这与多次加载表格单元有关。这是我的代码,该代码调用要显示的单元格:

UITableView

然后是func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let event = events[indexPath.row] let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell") as! eventCell cell.setEvent(event: event) return cell } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { //This code right here is mostly just for rounded edges but maybe the issue is here? let verticalPadding: CGFloat = 15 let maskLayer = CALayer() maskLayer.cornerRadius = 10 maskLayer.backgroundColor = UIColor.black.cgColor maskLayer.frame = CGRect(x: cell.bounds.origin.x, y: cell.bounds.origin.y, width: cell.bounds.width, height: cell.bounds.height).insetBy(dx: 0, dy: verticalPadding/2) cell.layer.mask = maskLayer } 类中的代码,其中包含显示代码的所有方向:

eventCell

这是问题的屏幕截图。如您所见,灰色的@IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var titleLabel: UILabel! // and a few more outlets func setEvent(event: event) { dateLabel.text = event.date titleLabel.text = event.title //and a few more of those^^ you get the idea db.collection("events").document(event.docID).getDocument { (document, error) in if let document = document, document.exists { let chairArray = document.data()!["chairPeople"] as! Array<String> for person in chairArray { if (person == event.email) { self.eventInfoButton.backgroundColor = UIColor(white: 50, alpha: 1) self.dateLabel.textColor = UIColor(white: 50, alpha: 1) self.titleLabel.textColor = UIColor(white: 50, alpha: 1) self.descriptionLabel.textColor = UIColor(white: 50, alpha: 1) self.eventInfoButton.setTitle("Chairsperson tools", for: .normal) self.contentView.backgroundColor = UIColor(red:0.26, green:0.55, blue:0.79, alpha:1.0) } } } } // This code right here ^^ grabs from the database and modifies the look if a conditon is met. If the condition is met, the text becomes distorted } 没问题,但蓝色的像素被像素化了(这是根据数据库中的信息完成工作的像素)。 Screenshot of the blue pixilated UITableViewCell

1 个答案:

答案 0 :(得分:0)

我会说这是您的maskLayer,就像您在评论中提到的那样。我怀疑即使细胞生长或收缩,层的大小也会保持固定大小。

我将在这里使用一个视图而不是一个图层,并将此新视图设置为新的contentView,如果您决定向圆角的contentView添加阴影,这也将有所帮助。

cell.backgroundColor = .clear
cell.contentView.backgroundColor = .clear

let shapeContentView = UIView( frame:cell.bounds.insetBy( dx:0.0, dy:7.5))
shapeContentView.masksToBounds = true
shapeContentView.cornerRadius = 10.0
shapeContentView.backgroundColor = .blue
cell.contentView.addSubview( shapeContentView)

将所有其他标签和按钮添加到shapeContentView