swift - динамический список элементов внутри tableViewCell - PullRequest
0 голосов
/ 06 февраля 2019

Как лучше добавлять динамический список элементов в UITableViewCell с showMore / showLess ?

UITableView внутри UITableViewCell

UITableView inside UITableViewCell

Я использовал табличное представление внутри UITableViewCell, но у меня проблема со строкой перезагрузки в indexPath , вызывающей скачкообразную прокрутку.

TableViewController Code:

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource , delegatCell {

    @IBOutlet weak var tableView: UITableView!

    var elementsArray: [data] = []



    override func viewDidLoad() {
        super.viewDidLoad()
        for i in 1...20 {
            elementsArray.append(data.init(title: "title\(i)", date: "8:00 PM", isShowMoreClicked: false, elements: ["ToDo\(i)","Aqraa\(i)","ToDo\(i)","ToDo\(i)" , "Mobile Team\(i)" , "Testing Data\(i)"]))
            // Do any additional setup after loading the view, typically from a nib.
        }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let obj = elementsArray[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! expandedTableViewCell
        cell.initializeCell()
        print(obj.elements)
        cell.data = obj.elements
        cell.delegate = self
        cell.dateLabel.text = obj.date
        cell.titleLabel.text = obj.title
        cell.showMore.titleLabel?.font = reqularDynamicFont()
        cell.showMore.titleLabel?.adjustsFontSizeToFitWidth = true
        cell.showMore.tag = indexPath.row
        cell.isShowMoreClicked = obj.isShowMoreClicked
        cell.tableView.reloadData()

        self.view.layoutIfNeeded()

        return cell

    }

    func reqularDynamicFont()-> UIFont{
        let font = UIFont.systemFont(ofSize: 15, weight: .regular)
        let fontMetrics = UIFontMetrics(forTextStyle: .caption1)
        return  fontMetrics.scaledFont(for: font)

    }


    func reloadTableView(indexpath: IndexPath , isShowMoreClicked: Bool) {
        elementsArray[indexpath.row].isShowMoreClicked = isShowMoreClicked
        tableView.reloadRows(at: [indexpath], with: .automatic)

    }

}

ExpandedTableViewCell с внутренним представлением таблицы. Код:

import UIKit
protocol delegatCell {
    func reloadTableView(indexpath: IndexPath , isShowMoreClicked: Bool)

}
class expandedTableViewCell: UITableViewCell , UITableViewDelegate , UITableViewDataSource{
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var showMore: UIButton!

    var data : [String] = []
    var initalNum = 2
    var isShowMoreClicked: Bool = false
    var delegate:delegatCell?

    func initializeCell(){
        data.removeAll()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isShowMoreClicked{
             return data.count
        }else{
            return initalNum
        }

    }

    @IBAction func reloadData(_ sender: UIButton) {
        delegate?.reloadTableView(indexpath: IndexPath(row: sender.tag, section: 0), isShowMoreClicked: !isShowMoreClicked)

    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print(data)
        let cell = tableView.dequeueReusableCell(withIdentifier: "simpleTableViewCell", for: indexPath) as! simpleTableViewCell
        cell.descLabel.text = data[indexPath.row]

        return cell

    }

}

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

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