Авторазбивка заголовка и ячейки в программно UITableView - PullRequest
1 голос
/ 01 марта 2020

У меня UIViewTable создан программно, я настроил заголовки и внешний вид ячейки с помощью Extension. Все, что мне нужно, это сделать большое количество текстов, отображаемых в заголовке / ячейке, для просмотра:

lineBreakMode = NSLineBreakMode.byWordWrapping // enable multi line
numberOfLines = 0 // for Automatic size

Я почти все использовал, но ничего не работает.

Я использовал:

self.tableView.estimatedRowHeight = 200.0
self.tableView.rowHeight = UITableView.automaticDimension

Я положил:

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
    }

Я также сделал:

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

Кажется, ничего не работает

вот мое расширение:

расширение indexController {

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let subDatas = sections[section].sub_catigories // [1]
        return subDatas?.count ?? 0


    }



    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let currentSection = sections[indexPath.section]
        let currentSubdata = currentSection.sub_catigories?[indexPath.row]
        //print(currentSubdata!.id)


        let vc = indexControllerTwo()
        vc.catNumber = currentSubdata!.id
        vc.sectionTitle = currentSubdata?.name
        navigationController?.pushViewController(vc, animated: true)

    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath)

        // [2]
        let currentSection = sections[indexPath.section]
        let currentSubdata = currentSection.sub_catigories![indexPath.row]

        // use listCell
        guard let titleCell = cell as? listCell else {
            return cell
        }

        titleCell.titleLabel.text = currentSubdata.name
        titleCell.listCount.text = "\(currentSubdata.number_of_subcatigories ?? 0)"

//        titleCell.titleLabel.numberOfLines = 3
//        titleCell.titleLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
//        titleCell.titleLabel.baselineAdjustment = .alignCenters
//        titleCell.titleLabel.adjustsFontSizeToFitWidth = true


//        self.tableView.estimatedRowHeight = 200.0
//        self.tableView.rowHeight = UITableView.automaticDimension



        cell.layer.backgroundColor = UIColor.clear.cgColor

        return cell
    }

Обратите внимание: listCell только для настройки и ограничения, и вот он:

import UIKit

class listCell: UITableViewCell {

    var safeArea: UILayoutGuide!
    let imageCell = UIImageView()
    let titleLabel = UILabel()
    let subTitleLabel = UILabel()
    let listCount = UILabel()



    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupView()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupView(){
        safeArea = layoutMarginsGuide
        setupTitleLabel()
        setupListCount()

    }



    func setupTitleLabel(){
        addSubview(titleLabel)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            titleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
            titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 7)
            ])


        titleLabel.font = UIFont(name: "verdana-Bold", size: 16)



    }



    func setupListCount(){
        addSubview(listCount)
        listCount.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            listCount.leadingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -30),
            listCount.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: -11)
            ])


        listCount.font = UIFont(name: "verdana", size: 10)

    }




}

Пожалуйста, помогите мне сделать заголовок и текстовое поле ячейки для автоматического изменения размера. Спасибо за ваше время.

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