0

What is the best practice to add dynamic list of items in UITableViewCell with showMore/showLess?

UITableView inside UITableViewCell

UITableView inside UITableViewCell

I used table view inside my UITableViewCell , but I have issue with reload row at indexPath causes jumpy scrolling.

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 with inner table view Code :

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

    }

}

I used auto layout to support dynamic height.

Mohammad Ghanem
  • 688
  • 6
  • 20
ghadeer aqraa
  • 159
  • 2
  • 11
  • I didn't understand why you call `cell.tableView.reloadData()` from `cellForRow..` event, shouldn't it be part of some call from TableViewController ? e.g., you set data source to inner tableview in `cellForRow..` event of TableViewController and once you call reload for main tableView, send message to inner tableview like this: https://stackoverflow.com/questions/41269300/reloading-tableview-from-collectionviewcell – NeverHopeless Feb 06 '19 at 12:23
  • Have a look here: https://stackoverflow.com/a/50964877/8918347 – Govind Kumawat Feb 06 '19 at 12:28

0 Answers0