Добавление ограничений сверху и снизу приводит к сжатию UILable - PullRequest
0 голосов
/ 14 июля 2020

Программно я создал пользовательский UITableViewCell и попытался центрировать два UILabel по вертикали внутри него. Но в итоге UILabel оказался раздавленным. То же самое в Interface Builder с ячейкой прототипа работает хорошо. Что не так с моим кодом?

Класс ячейки настраиваемого представления

import UIKit

class TopViewCell: UITableViewCell {
    let df: DateFormatter = {
        let df = DateFormatter()
        df.dateFormat = NSLocalizedString("DATE_WEEKDAY", comment: "show date and weekday")
        return df
    }()
    var dateLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    var costLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let margin = contentView.layoutMarginsGuide
        
        contentView.addSubview(dateLabel)
        dateLabel.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
        dateLabel.topAnchor.constraint(equalTo: margin.topAnchor).isActive = true
        dateLabel.bottomAnchor.constraint(equalTo: margin.bottomAnchor).isActive = true
        
        contentView.addSubview(costLabel)
        costLabel.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
        costLabel.topAnchor.constraint(equalTo: dateLabel.topAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        dateLabel.text = df.string(from: Date())
        costLabel.text = "total: five thousand"
    }
}


Класс настраиваемого UITableViewController

import UIKit

class ItemViewController: UITableViewController {
    var itemStore: ItemStore!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(TopViewCell.self, forCellReuseIdentifier: "top_cell")
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemStore.allItems.count + 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: UITableViewCell!
        
        if indexPath.row == 0 {
            cell = tableView.dequeueReusableCell(withIdentifier: "top_cell", for: indexPath)
        } else {
            cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = itemStore.allItems[indexPath.row - 1].name
            cell.textLabel?.font = cell.textLabel!.font.withSize(30)
            cell.detailTextLabel?.text = "$\(itemStore.allItems[indexPath.row - 1].valueInDolloar)"
        }
        
        return cell
    }
}

enter image description here

введите описание изображения здесь

1 Ответ

1 голос
/ 14 июля 2020

Ваш TopViewCell неправильно изменяет размер автоматически, потому что вы устанавливаете текст в layoutSubviews(). Переместите эти две строки в init, и он будет иметь правильный размер:

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    let margin = contentView.layoutMarginsGuide
    
    contentView.addSubview(dateLabel)
    dateLabel.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
    dateLabel.topAnchor.constraint(equalTo: margin.topAnchor).isActive = true
    dateLabel.bottomAnchor.constraint(equalTo: margin.bottomAnchor).isActive = true
    
    contentView.addSubview(costLabel)
    costLabel.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
    costLabel.topAnchor.constraint(equalTo: dateLabel.topAnchor).isActive = true

    // set the text here
    dateLabel.text = df.string(from: Date())
    costLabel.text = "total: five thousand"
}

В качестве примечания, вы должны указать класс при использовании TopViewCell:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "top_cell", for: indexPath) as! TopViewCell
        return cell
    }
        
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = itemStore.allItems[indexPath.row - 1].name
    cell.textLabel?.font = cell.textLabel!.font.withSize(30)
    cell.detailTextLabel?.text = "$\(itemStore.allItems[indexPath.row - 1].valueInDolloar)"

    return cell
}

В качестве другого примечания ... вы можете создать две ячейки прототипа в своей раскадровке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...