chirag shah chirag shah - 3 months ago 16
Swift Question

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

Answer

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.