UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

self sizing cell with uitableviewdiffabledatasource

Forums > iOS

I have a detail ViewController whoose cells are defined by a custom uitableviewdiffabledata like this:

{ (_, indexPath, item) -> UITableViewCell? in
            let color = UIColor(named: "blue")!
            if let _ = item as? TextFieldItem, let cell = tableView.dequeueReusableCell(withIdentifier: "textField", for: indexPath) as? TextFieldTableViewCell {
                cell.textField.text = recipe.wrappedValue.name
                cell.textField.placeholder = NSLocalizedString("name", comment: "")
                cell.selectionStyle = .none
                cell.textChanged = nameChanged
                cell.backgroundColor = color
                return cell
            } else if let imageItem = item as? ImageItem, let imageCell = tableView.dequeueReusableCell(withIdentifier: "image", for: indexPath) as? ImageTableViewCell {
                imageCell.setup(imageData: imageItem.imageData)
                return imageCell
            } else if let _ = item as? AmountItem, let amountCell = tableView.dequeueReusableCell(withIdentifier: "times", for: indexPath) as? AmountTableViewCell{
                amountCell.setUp(with: recipe.wrappedValue.timesText, format: formatAmount)
                amountCell.backgroundColor = color
                return amountCell
            } else if item is InfoItem {
                return  InfoTableViewCell(infoText: Binding(get: {
                    return recipe.wrappedValue.info
                }, set: updateInfo), reuseIdentifier: "info")
            } else if let stripItem = item as? InfoStripItem, let infoStripCell = tableView.dequeueReusableCell(withIdentifier: "infoStrip", for: indexPath) as? InfoStripTableViewCell {
                infoStripCell.setUpCell(for: stripItem)
                return infoStripCell
            } else if let stepItem = item as? StepItem {
                let stepCell = StepTableViewCell(style: .default, reuseIdentifier: "step")
                stepCell.setUpCell(for: stepItem.step)
                return stepCell
            } else if let detailItem = item as? DetailItem, let cell = tableView.dequeueReusableCell(withIdentifier: "detail", for: indexPath) as? DetailTableViewCell {
                let title = NSAttributedString(string: detailItem.text, attributes: [.foregroundColor : UIColor.label])
                cell.textLabel?.attributedText = title
                cell.accessoryType = .disclosureIndicator
                cell.backgroundColor = color
                return cell
            }
            return UITableViewCell()
        }

and i want to make the InfoTableViewCell which is defined like this:

class InfoTableViewCell: UITableViewCell {
    @Binding private var infoText: String

    private var textView = UITextView()

    init(infoText: Binding<String>, reuseIdentifier: String?) {
        self._infoText = infoText
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        setup()
    }

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

    private func setup() {
        self.addSubview(textView)
        textView.fillSuperview()

        textView.text = infoText
        textView.delegate = self

        textView.backgroundColor = UIColor(named: "blue")!
        textView.font = UIFont.preferredFont(forTextStyle: .body)
    }

}

extension InfoTableViewCell: UITextViewDelegate {

    func textViewDidChange(_ textView: UITextView) {
        self.infoText = textView.text
    }

}

and i want to make this cell to resize based on the text thats in the textField. Any tips on how to do that ? P. S. I am using the LBTA tools for filling the contents of the cell with the textField.

3      

Can you try calling tableView.beginUpdates() before assigning self.infoText and calling tableView.endUpdates() just after that line?

3      

I do not see how this would make the self sizing working just by figuring around with some updates which is already working fine the problem is that the InfoTableViewCell should grow while the text grows.

3      

Try setting the row heigth to automatic on your tableview and set a estimated row heigth

        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 70

3      

does not work it still looks the same.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.