Setting UITableViewCell Height to fit ios swift 3 content

I am creating a tableView and I am creating a dynamic UILabel in CellforRowat Now the UILabel can be n times and I want to set the height of the cell. According to the label created in the TableViewCell.

Please take a screenshot below: enter image description here

I already tried the solution but it doesn't work for me

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension;

    }
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300

    }

      

Also I tried adding rowheight and score row to viewdidload but didn't work for me like

tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300

      

I created my label in the forrowat cell like this:

   let AllCompanydict = ["CommonKey": CommonKey , "Company": companyname, "Participants": self.Contacts] as [String : Any]
                            self.AllCompanydicArray.append(AllCompanydict)


     for i in 0..<AllCompanydicArray.count {
                let commonkey = ((AllCompanydicArray[i] as NSDictionary).value(forKey: "CommonKey") as! String)
                if(commonkey == cell.lblCommonKey.text){


                if(isFirstTime == false){
                    RowHeight = RowHeight + 30.0
                    CellHeight = CellHeight + 30.0
                }
                else{
                    isFirstTime = false
                }
                let Company = (AllCompanydicArray[i] as NSDictionary).value(forKey: "Company") as! String
                let Contacts = (AllCompanydicArray[i] as NSDictionary).value(forKey: "Participants") as! [String]
               // section.append(Company)
                //items.append(Contacts)
                 var newLabel = UILabel(frame: CGRect(x: 10.0, y: RowHeight, width:cell.frame.width, height: 30.0))
                newLabel.text = Company
                newLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 13)
                cell.addSubview(newLabel)

                for j in 0..<Contacts.count {
                    RowHeight = RowHeight + 30.0
                    CellHeight = CellHeight + 30.0

                newLabel = UILabel(frame: CGRect(x: 10.0, y: RowHeight, width:30, height: 30.0))
                    newLabel.text = CaptionName(Name: Contacts[j])
//"RC"
                newLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 11)
                newLabel.textAlignment = .center
                newLabel.backgroundColor = getColorByHex(rgbHexValue: 0x002366, alpha: 0.9)// UIColor(red: 0.0, green: 14.0, blue: 40.0, alpha: 100.0)
                newLabel.layer.cornerRadius = (newLabel.frame.size.width) / 2
                newLabel.layer.borderWidth = 1.0
                newLabel.clipsToBounds = true
                newLabel.textColor = UIColor.white
                newLabel.layer.borderColor = UIColor.white.cgColor

                cell.addSubview(newLabel)

                newLabel = UILabel(frame: CGRect(x: 50, y: RowHeight, width:cell.frame.width-30, height: 30.0))
                newLabel.text = Contacts[j]//"Raghav Chopra"
                newLabel.font = UIFont(name:"HelveticaNeue", size: 13)
                cell.addSubview(newLabel)

                }
            }
        }

      

but when i scroll down my view is distorted this way

enter image description here Please let me know the way forward.

Thank you in advance

How can i solve this problem

+3


source to share





All Articles