Как настроить UITableView и его ячейку, чтобы получить четкий цветовой разделитель, а разная длина ячейки зависит от заголовка? - PullRequest
0 голосов
/ 07 октября 2019

Пока у меня есть 7 ячеек таблицы без разделителя и фиксированной длины для всех них. Как 1) добавить несколько четких цветовых разделителей между ними и 2) установить оранжевый цвет фона ячейки, но цвет должен быть только под меткой, текст которой будет иметь разную длину?

Вот мой текущий код:

class CafesView: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    let cafes = [
        "Banana Joe's",
        "College Eight Cafe",
        "Global Village",
        "Iveta",
        "Stevenson Coffee House",
        "Terra Fresca",
        "Vivas"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cafesCell = tableView.dequeueReusableCell(withIdentifier: "cafeCell", for: indexPath)
        cafesCell.textLabel?.text = cafes[indexPath.row]
        return cafesCell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.frame.height / 7
    }

    @IBAction func closeTapped(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

}

class TableViewCell: UITableViewCell {

    override func layoutSubviews() {
        super.layoutSubviews()

        self.backgroundColor = .systemOrange
        self.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10)

   }

}

1 Ответ

1 голос
/ 07 октября 2019

Добавьте эту строку, чтобы удалить разделительную линию

tableView.separatorStyle = .none

И добавьте одну метку (здесь я добавил lblCafes) в UITableViewCell и установите цвет фона метки в оранжевый цвет.

Ваш код будет,

    class CafesView: UIViewController, UITableViewDelegate, UITableViewDataSource {

        @IBOutlet weak var tableView: UITableView!

        let cafes = [
            "Banana Joe's",
            "College Eight Cafe",
            "Global Village",
            "Iveta",
            "Stevenson Coffee House",
            "Terra Fresca",
            "Vivas"
        ]

        override func viewDidLoad() {
            super.viewDidLoad()

            tableView.separatorStyle = .none

            tableView.delegate = self
            tableView.dataSource = self
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 7
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cafesCell = tableView.dequeueReusableCell(withIdentifier: "cafeCell", for: indexPath) as? TableViewCell
            cafesCell?.lblCafes?.text = cafes[indexPath.row]
            return cafesCell ?? UITableViewCell()
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return tableView.frame.height / 7
        }

        @IBAction func closeTapped(_ sender: UIButton) {
            self.dismiss(animated: true, completion: nil)
        }

    }

    class TableViewCell: UITableViewCell {

        @IBOutlet weak var lblCafes: UILabel!

        override func layoutSubviews() {
            super.layoutSubviews()

            self.lblCafes.backgroundColor = .systemOrange
            self.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10)

       }
    }
...