Swift dynamic tableview in proportion to label height filled with text

核能气质少年 提交于 2019-12-12 02:19:08

问题


Hello I'm trying to make the table view with custom cell. The main goal of making it is that I resize the height of tableview cell in proportion to label height which is changed by the size of text. when I run my app with below code, it looks go well. However getStringHeight methods sometimes don't recognize the line of the cell. Even if the number of line is just one, that method allocates the two line of cell which makes the cell unnecessary space.

I think the if it would recognizes two lines, line is over 3/4 filled with text in one line. I think I use Korean font included in the Apple SD Gothic (Family). But I can enter the exact name into UIFont(name: "", size: ) because it doesn't recognize it.

Can you help me? it's so important project. I stayed up all night but I haven't come up with idea to fix this problems.

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    let rowNum = downloadedContents!.count - indexPath.row - 1

    let sizeFactory = UINib(nibName: "DownLoadPlayViewControllerCell", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? DownLoadPlayViewControllerCell
    sizeFactory!.frame.size.height = 300
    var bounds = CGSize(width: tableView.bounds.width, height: 78.5)
    let pod = self.pods[rowNum]
    if(sizeFactory != nil)
    {
        let top = sizeFactory?.programLabel.frame
        let bottom = sizeFactory?.dateFilesizeLabel.frame


        var labelSize = getStringHeight(pod.contentTitle!, fontSize: 13.0, width: sizeFactory!.titleLabel.frame.width)

        let totalCellHeight = labelSize + top!.height + bottom!.height + 28.0
        bounds.height = totalCellHeight
    }


    return bounds.height


}

func getStringHeight(mytext: String, fontSize: CGFloat, width: CGFloat) -> CGFloat
{
    let font = UIFont(name: "Apple SD Gothic Neo", size: fontSize)
    let size = CGSizeMake(width,CGFloat.max)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .ByWordWrapping;
    let attributes = [NSFontAttributeName:font! as UIFont,
                      NSParagraphStyleAttributeName:paragraphStyle.copy()]

    let text = mytext as NSString
    let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil)
    return rect.size.height
}

回答1:


I may suggest that you use:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60

This will autosize the height of the cell without looking for the height of the text. You can put these two lines on viewDidLoad as for the estimated row height, it helps you out in rendering.




回答2:


Try this code:

Note: Both method should work.Tested in Swift 3.

Method 1:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    tableView.estimatedRowHeight = 44.0 // standard tableViewCell height
    tableView.rowHeight = UITableViewAutomaticDimension

 return yourArrayName.count

    }

Method 2:

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

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

Note: You may need to put this code inside your cellForRowAt

  yourCellName.sizeToFit()


来源:https://stackoverflow.com/questions/40189092/swift-dynamic-tableview-in-proportion-to-label-height-filled-with-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!