与填充文本的标签高度成比例的 Swift 动态 tableview
您好,我正在尝试使用自定义单元格制作表格视图.制作它的主要目标是我根据文本大小改变的标签高度按比例调整表格单元格的高度.当我用下面的代码运行我的应用程序时,它看起来很顺利.但是 getStringHeight 方法有时无法识别单元格的行.即使行数只有一行,该方法也会分配两行单元格,这使得单元格不需要空间.
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.
我认为如果它可以识别两行,则行超过 3/4,一行中填充了文本.我想我使用 Apple SD Gothic (Family) 中包含的韩语字体.但是我可以在 UIFont(name: "", size: ) 中输入确切的名称,因为它无法识别.
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
}
试试这个代码:
注意:这两种方法都应该有效.在 Swift 3 中测试过.
Note: Both method should work.Tested in Swift 3.
方法一:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.estimatedRowHeight = 44.0 // standard tableViewCell height
tableView.rowHeight = UITableViewAutomaticDimension
return yourArrayName.count
}
方法二:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
注意:您可能需要将此代码放入cellForRowAt
Note: You may need to put this code inside your cellForRowAt
yourCellName.sizeToFit()