NotationMaster
NotationMaster

Reputation: 410

Why is the cell not resizing with its content?

I have read all of the similar questions I could find here and applied all of the proposed solutions ... none of them works.

The table view is a grouped-one, the number of lines for the textLabel and the detailTextLabel is set to 0 both in code and in the storyboard, the string data is made of a multiline string created via "\n" characters...

Setting tableView.estimatedRowHeight to something else rather than the automatic 43.5 and then tableView.rowHeight = UITableView.automaticDimension as suggested in some other questions does absolutely nothing... or if I put something like 100, it will resize all the cells instead of just the ones which have multiple lines of text.

I am unfamiliar with this type of behaviour from table views: in my experience, simply putting the number of lines to 0 for the labels just worked...

Why do I get this result?

shrank image

EDIT: I have also tried to implement the heightForRowAt method, passing in the desired sections and rows but nothing at all changed ... I am lost.

EDIT (2): Removed previous snippet and inserted ViewController source code: the only thing I can show that is relevant to the cell is this, as the only property is an instance of a custom data type that is populating the table view (and it is being very good at that) and viewDidLoad() has nothing of mine added inside of it.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.numberOfLines = 0
        cell.detailTextLabel?.numberOfLines = 0

        func configureTextLabels(withString labelText: String, and detailText: String) {
            cell.textLabel?.text = labelText
            cell.detailTextLabel?.text = detailText
        }

        if let country = detailCountry {
            switch indexPath.section {
            // insert 14 different cases here
            case 14: 
                func populateRows() {
                    let regionalBlocs = country.regionalBlocs

                    for i in 0 ..< regionalBlocs.count {
                        switch indexPath.row {
                        case 0 + (i * 4): configureTextLabels(withString: "Name: ", and: "\(regionalBlocs[i].name)")
                        case 1 + (i * 4): configureTextLabels(withString: "\tAcronym: ", and: "\(regionalBlocs[i].acronym)")
                        case 2 + (i * 4):
                            var detailText = ""
                            let otherNames = regionalBlocs[i].otherNames

                            if !otherNames.isEmpty {
                                for name in otherNames {
                                    if name == otherNames.last {
                                        detailText += name
                                    } else {
                                        detailText += "\(name)\n"
                                    }
                                }
                                configureTextLabels(withString: "\tOther names: ", and: detailText)
                            } else {
                                configureTextLabels(withString: "", and: "No other names found")
                            }
                        case 3 + (i * 4):
                            var detailText = ""
                            let otherAcronyms = regionalBlocs[0].otherAcronyms

                            if !otherAcronyms.isEmpty {
                                for acronym in otherAcronyms {
                                    if acronym == otherAcronyms.last {
                                        detailText += acronym
                                    } else {
                                        detailText += "\(acronym)\n"
                                    }
                                }
                                configureTextLabels(withString: "\tOther acronyms: ", and: detailText)
                            } else {
                                configureTextLabels(withString: "", and: "No other acronym found")
                            }
                        default: break
                        }
                    }
                }

                let regionalBlocs = country.regionalBlocs

                if regionalBlocs.isEmpty {
                    cell.textLabel?.text = ""
                    cell.detailTextLabel?.text = "No regional bloc data found for this country"
                } else {
                    populateRows()
                }
            }
        return cell
        }

Upvotes: 0

Views: 259

Answers (1)

Thanh Vu
Thanh Vu

Reputation: 1739

You can look answer in here: Add multiple lines to detailTextLabel in UITableViewCell

But I suggest you create custom cell for this case, your issue is UIKit's bug.

Upvotes: 1

Related Questions