ios - 如何调整自定义tableView Cell中的标签高度和宽度

标签 ios swift uitableview uilabel

我有一个可扩展的tableView,当我扩展一个部分时,其中会出现三个单元格。在第一个单元格中只有名称,而在第二个单元格中只有名称。它的内容很大。现在我想根据内容自动调整这个标签的高度和宽度。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tblView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell

        let dataArrayTblView = dataArrayForTableView
        let titleName = dataArrayTblView.valueForKey("name")
        let dueDate = dataArrayTblView.valueForKey("deadlinedate")
        let description = dataArrayTblView.valueForKey("description")

        cell.titleLabel.text = titleName[indexPath.row] as AnyObject! as! String!
        cell.dueDateLabel.text = dueDate[indexPath.row] as? String
        cell.descriptionLabel.text = description[indexPath.row] as? String

        cell.descriptionLabel.sizeToFit()


        cell.textLabel?.backgroundColor = UIColor.clearColor()
        cell.selectionStyle = .None
        return cell
    }

但没有获得完整的内容

enter image description here

最佳答案

尝试设置这个。它会自动为您调整行的高度。如果它不起作用,那么 Storyboard中的约束就有问题。

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

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

关于ios - 如何调整自定义tableView Cell中的标签高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911740/

相关文章:

ios - swift中的负数模

iphone - iOS:如何将像素从源复制到目标矩形?

ios - 签署 PhoneGap 构建应用程序以进行分发。无法下载

ios - Duet - 并排合并 2 个视频

arrays - 将大数组拆分为两个数组

ios - VC 用 UISlider 更改 VC2 上的文本字段

iphone - objective-c 中UITableView中的UILabel

iOS 11 TableView 到导航栏下方的 UIView 奇数空间

ios - 我可以在 cordova 中为 iPad 和 iPhone(通用)应用程序设置不同的方向吗?

ios - 无法在 Xcode 4.2 中使用 Apple LLVM 3.0 编译 cocos2d-2.0-beta