首页 > 解决方案 > 单元格尺寸 rowHeight 大小

问题描述

我需要调整我的UITableViewCell尺寸。我通过更改 rowHeight.dimension 来做到这一点,但它不起作用。

class SobreViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight=150
    }



    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 9
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell();
        switch indexPath.row{

        case 1:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "s1") else {return UITableViewCell()}
            cell.textLabel?.text = "A aplicação móvel MyInfo@IPLeiria permite-te consultar de forma simples e rápida alguma da tua informação  académica, quer lista de unidades curriculares a que estás  inscrito no teu ano letivo atual, quer a data/hora das tuas  avaliações, quer o teu horário, as tuas notas ou até  mesmo a referência para o pagamento das propinas."
            return cell;
        case 2:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "s2") else {return UITableViewCell()}
            cell.textLabel?.text = "MyInfo@IPLeiria foi desenvolvida no âmbito de projeto final  de Licenciatura de Engenharia Informática"
            return cell;
        case 3:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "versaoAplicacao") else {return UITableViewCell()}
            cell.detailTextLabel?.text = "1.0"
            return cell;
        case 4:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "s4") else {return UITableViewCell()}
            cell.textLabel?.text = "Tecnologia"
            return cell;
        case 5:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "versaoFramework") else {return UITableViewCell()}
            cell.detailTextLabel?.text = "1.0.0"
            return cell;
        case 6:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "versaoNode") else {return UITableViewCell()}
            cell.detailTextLabel?.text = "5.12.0"
            return cell;
        case 7:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "versaoCordova") else {return UITableViewCell()}
            cell.detailTextLabel?.text = "5.4.1"
            return cell;
        case 8:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "s8") else {return UITableViewCell()}
            cell.textLabel?.text = "Podes consultar toda a informação em modo online e em modo offline"
            return cell;
        case 9:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "s9") else {return UITableViewCell()}
            cell.textLabel?.text = "Copyright Politécnico de Leiria © 2019"
            return cell;
        default:
            break;
        }
        return cell;
    }
}

标签: iosswift

解决方案


你也需要实现 UITableViewDelegate 的这个功能:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
}

同样对于 UITableViewCell,您需要将标签设置为多行:

cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = UILineBreakModeWordWrap

推荐阅读