迅速-tableViewCell

时间:2019-02-06 11:07:13

标签: ios swift uitableview

通过 showMore / showLess UITableViewCell 中添加动态项目列表的最佳实践是什么?

UITableViewCell内的UITableView

UITableView inside UITableViewCell

我在UITableViewCell中使用了表格视图,但是我在 indexPath 处重新加载行时遇到了问题,导致跳动滚动。

TableViewController代码:

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

    }

}

我使用自动布局来支持动态高度。

0 个答案:

没有答案
相关问题