【问题标题】:Swift: Pixelated Text In UITableViewCellSwift:UITableViewCell 中的像素化文本
【发布时间】:2019-01-28 06:38:00
【问题描述】:

我正在更改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
}

这是问题的屏幕截图。如您所见,灰色的Cells 没有问题,但蓝色的是像素化的(这是根据数据库中的信息完成工作的那个)。

【问题讨论】:

    标签: ios swift uitableview uikit uilabel


    【解决方案1】:

    我会说这是您的 maskLayer,正如您在 cmets 中提到的那样。我怀疑即使单元格增长或缩小,层大小也保持固定大小。

    我会在这里使用视图而不是图层,并将这个新视图设为您的新 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

    【讨论】:

    • 似乎这很有意义,但我没有添加标签和按钮,而是使用情节提要中的原型单元格。那我该怎么做呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多