0

I have to do this implementation, I need a list of cells representing month periods, where each one is collapsed, and when clicked it shows its content, I used two cells prototypes based on some tutorials I found but I'm really new into swift programming, I can't get the expected result, I share some screens and actual code. Hope someone could help me.

design

class BillingListCell: UITableViewCell{
    @IBOutlet weak var billWrapper: UIView!

    @IBOutlet weak var billTotal: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
class BillingListHeaderCell: UITableViewCell{


    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var numberLabel: UILabel!
    @IBOutlet weak var statusButton: UIButton!

    func setExpanded() {
        statusButton.setImage(UIImage(systemName: "chevron.up"), for: .normal)
    }

    func setCollapsed() {
        statusButton.setImage(UIImage(systemName: "chevron.down"), for: .normal)
    }
}

class BillingListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var billingListTableView: UITableView!

    var paymentArray: [String] = ["data","data2", "data3"]
    private let numberOfActualRowsForSection = 1

    func numberOfSections(in tableView: UITableView) -> Int {
        return paymentArray.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // First will always be header
        return false ? (1+numberOfActualRowsForSection) : 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if(indexPath.row == 0){
            let cell = tableView.dequeueReusableCell(withIdentifier: "BillingListHeaderCell", for: indexPath) as! BillingListHeaderCell

            cell.setCollapsed()
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "BillingListCell", for: indexPath) as! BillingListCell

            cell.billWrapper.layer.cornerRadius = 15
            cell.billWrapper.layer.borderWidth = 1
            cell.billWrapper.layer.borderColor = UIColor.blue.cgColor
            cell.billTotal.text = "1234"
            return cell
        }

    }
BetoCuevas
  • 123
  • 8
  • So this is supposed to be a section header that can be tapped to show / hide the cells in the section? – matt Oct 24 '19 at 18:57
  • Yeah, it will always have only one cell content, thats why i added only two prototype cells, but i thought it will cycle the two prototypes with each element, i think that was my mistake. Instead in each iteration it either draws one or another. – BetoCuevas Oct 24 '19 at 19:16
  • I am trying to get you to explain what your code is attempting to do. Looking at the screen shots doesn’t tell me what is supposed to be happening. – matt Oct 24 '19 at 19:55
  • Its like a list of month periods, so when you tap one of them, it shows the data as in the first image, you tap again and it collapses, i used two proto cells, one for the tappable header and other for the actual info. But I got confused in the process of implementing that functionality when dequeueing the cells. – BetoCuevas Oct 24 '19 at 22:32

0 Answers0