Search code examples
swiftxcodeuitableviewtableviewexpandable

Is there a way to fit long texts inside the tableview cell which is expandable cell? (programmatically)


my question is that I want to write long label into the table cell, but I can't find any way to fix. I tried numberOfLines = 0, UITableView.automaticDimension codes but none of this solve my problem. My tableView cell will expand when clicking and other label which is cevap label will appear. Expand thing working correctly but labels can't fit in screen. Here my anchor codes (I think I have a problem about that but I can't find any solution about 2-3 days.)

class expandingCell: UITableViewCell {
let cellView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.clear
    return view
}()

let soruLabel: UILabel = {
    let sorulabel = UILabel()
    sorulabel.textColor = .black
    sorulabel.font = UIFont.boldSystemFont(ofSize: 18)
    return sorulabel
}()

let cevapLabel: UILabel = {
    let cevaplabel = UILabel()
    cevaplabel.textColor = .black
    cevaplabel.font = UIFont.boldSystemFont(ofSize: 18)
    return cevaplabel
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setup()
}

func set(content: expandingTableCellForsss) {
    self.soruLabel.text = content.soru
    self.cevapLabel.text = content.expanded ? content.cevap : ""
}

func setup() {
    backgroundColor = UIColor.init(red: 245, green: 245, blue: 245, alpha: 1)
    addSubview(cellView)
    cellView.addSubview(soruLabel)
    cellView.addSubview(cevapLabel)

    cellView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: frame.width, height: 60)
    soruLabel.anchor(top: cellView.topAnchor, left: cellView.leftAnchor, bottom: nil, right: cellView.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 35, height: 35)

    cevapLabel.anchor(top: soruLabel.bottomAnchor, left: cellView.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 35)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

My custom cell for expanding. I didn't use storyboard so I have to do layout with coding. I would appreciate if you could help me.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "details2", for: indexPath) as! customCell
        let detailGelen = detailsModel[indexPath.row]
        cell.detailCellLabel.text = detailGelen.itemDetailName
        cell.priceLabel.text = detailGelen.itemDetailPrice!
        return cell
    }else {
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: expandingTableCellForsss.self), for: indexPath) as! expandingCell
        cell.set(content: dataSource[indexPath.row])
        return cell

    }

}

This code for not expand cells for section == 1 and

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //let detailGelen = detailsModel[indexPath.row]    BURADAN DATAYI ALICAK
    if indexPath.section == 0 {
        let cell = tableView.cellForRow(at: indexPath) as! customCell
        cell.count += 1
        cell.countLabel.text = "x \(cell.count)"
        cell.imageForCell.image = nil
    }else {
        print("naptın")

        let content = dataSource[indexPath.row]
        content.expanded = !content.expanded
        tableView.reloadRows(at: [indexPath], with: .automatic)

    }

}

this for didSelectItem and expanding cell is appear but not exactly I wish.

And lastly situation is :

enter image description here


Solution

  • Add right and bottom constraints to cevapLabel. width & height be nil.
    Set numberOfLines to 0.
    Use UITableViewAutomaticDimension.

    Now cevapLabel will set it's size according to the text.

    let cevapLabel: UILabel = {
        let cevaplabel = UILabel()
        cevaplabel.textColor = .black
        cevaplabel.font = UIFont.boldSystemFont(ofSize: 18)
        cevaplabel. numberOfLines = 0
        return cevaplabel
    }()
    
    
    
    func setup() {
        ...
        ...
    
        cevapLabel.anchor(top: soruLabel.bottomAnchor, left: cellView.leftAnchor, bottom: bottom: cellView.bottomAnchor, right: cellView.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: nil, height: nil)
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return 80 // Give estimated Height Fo rRow here 
     }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         return UITableViewAutomaticDimension
    }