根据内容自动更改单元格高度 - Swift

时间:2015-07-06 01:05:14

标签: ios swift uitableview

在Swift中使用UITableView,有人可以帮我根据标签,图片和描述自动更改单元格的高度吗?

所有信息都正确传递,我只需要帮助格式化。

我尝试使用cell.frame.size.height调整它,但这没有效果。我可以在故事板中更改单元格的大小,但如果可能的话,我希望它更具动态性。

提前谢谢!

我的手机如下:

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

        // Creator
        let creator = self.photos[indexPath.row].creator
        let user = UILabel()
        user.frame = CGRectMake(self.headerView.frame.origin.x, self.headerView.frame.origin.y + self.headerView.frame.height + 3, self.view.frame.width, 12)
        user.text = creator
        cell.addSubview(user)

        // Photo
        let picture = self.photos[indexPath.row].image()
        let imageView = UIImageView(image: picture!)
        imageView.frame = CGRectMake(user.frame.origin.x, user.frame.origin.y + user.frame.height, self.view.frame.width, 400)
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        cell.addSubview(imageView)

        // Photo description
        let description = UITextView()
        let des = self.photos[indexPath.row].photoDescription
        description.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageView.frame.height, self.view.frame.width, 65)
        if des != nil {
            description.text = des!
            description.font = UIFont.systemFontOfSize(16)
            description.editable = false
            description.scrollEnabled = false
        }
        cell.addSubview(description)

        cell.frame.size.height = user.frame.size.height + imageView.frame.size.height + description.frame.size.height + 30

        return cell
    }

2 个答案:

答案 0 :(得分:8)

为了使单元格自动变大,您需要做两件事:

  1. 使用autolayout排列单元格内的元素。底部空间和顶部空间限制将根据需要推动单元尺寸。
  2. tableView.rowHeight = UITableViewAutomaticDimension
  3. 中设置viewDidLoad

答案 1 :(得分:6)

如果您的手机高度取决于文字大小,请按以下步骤操作: 在ViewController.swift中添加方法

func calculateHeight(inString:String) -> CGFloat {
     let messageString = inString
     let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 15.0)]

     let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)

     let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 222.0, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)

      let requredSize:CGRect = rect
      return requredSize.height
}

设置文字标签的宽度 然后调用此函数:

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            heightOfRow = self.calculateHeight(inString: conversations[indexPath.row].description)

            return (heightOfRow + 60.0)
    }

对于基本单元格:

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

此功能不适用于自定义单元格。

如果您的单元格高度取决于图像高度,则返回图像高度+ someFloatValueAcordingToYourChoice。

希望它能奏效。

相关问题