Chirag Shah
Chirag Shah

Reputation: 3016

UITableViewAutomaticDimension applied on subtitle table view cell not working properly

I am making one view for the listing. It has dynamic length in subtitle. So i used UITableViewAutomaticDimension properties. I have not applied any constraint because both the label are table view cell properties so no need to make new custom cell.

But after running this project i marked that UITableViewAutomaticDimension not working properly. I don't know what i doing wrong. My code is like below

view did load

@IBOutlet weak var tblabc: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tblabc.separatorColor = UIColor.clearColor()

        tblabc.estimatedRowHeight = 44
        tblabc.rowHeight = UITableViewAutomaticDimension



        // Do any additional setup after loading the view.
    }

table view delegate and data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        if (cell == nil)
        {

            cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell")
            cell?.selectionStyle = .None
            cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15)
            cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13)
            cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0)
            cell?.detailTextLabel?.numberOfLines = 0
            cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail
            cell?.selectionStyle = .None

        }

        cell?.textLabel?.text = "Testing" + "\(indexPath.row)"
        cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh"
        cell?.imageView?.image = UIImage(named: "new_temp")

        return cell!
    }

View looks like below image

enter image description here

Upvotes: 3

Views: 886

Answers (4)

Jim75
Jim75

Reputation: 767

The problem should be fixed in iOS 11. A shortcut solution working just fine on lower versions is to fake the desired subtitle appearance within a Basic cell by setting its textlabel.attributedText to have a bolder first line(s) + \n + regular more lines.

Upvotes: 0

tiritea
tiritea

Reputation: 1279

The UITableViewAutomaticDimension height calculation for the built-in UITableViewCellStyles (all of them!) is basically 'broken' when you have a multi-line detailTextLabel. As Venkat Reddy commented, see my solution here: Autolayout ignores multi-line detailTextLabel when calculating UITableViewCell height (all styles)

[Someone with suitable privileges should consider flagging this question as a duplicate]

Upvotes: 3

Sumit singh
Sumit singh

Reputation: 2446

Check this Modify code :-

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
    if (cell == nil)
    {

        cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell")
        cell?.selectionStyle = .None
        cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15)
        cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13)
        cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0)
        cell?.detailTextLabel?.numberOfLines = 0
        cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail
        cell?.selectionStyle = .None

    }

    cell?.textLabel?.text = "Testing" + "\(indexPath.row)"
    cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh"
    cell?.imageView?.image = UIImage(named: "new_temp")

    return cell!
}

Please remove the HeightForRowAtIndex datasource Method.

Upvotes: 1

Chirag Patel
Chirag Patel

Reputation: 1481

try this code:

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

remove both line:

tblabc.estimatedRowHeight = 44
tblabc.rowHeight = UITableViewAutomaticDimension

Upvotes: -1

Related Questions