UITableViewCell 放置 Button,怎么让Cell的高度随着Button的文字长度自动增加

在UITableViewCell中动态生成了一个Button控件,在Button上放置的文字,怎么让Cell的高度能根据Button上文字的多少自动增加高度?

如图Button上文字有3行,Cell还是2行的高度。。。

图片描述

阅读 4.5k
2 个回答

设置约束,子view的top和父view的top之间间隔是0,bottom和bottom之间是0。子view的高度要自动适应。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)-> UITableViewCell {
。。。。。。。。。

let radio = xxRadioButtons[indexPath.section-1]?[indexPath.row-1]
radio?.setTitle(num2strDic[indexPath.row]! + "." + (a3tms[indexPath.section]?[indexPath.row])!, for: .normal)cell.contentView.addSubview(radio!)

        
        radio?.snp.makeConstraints{
            (make)->Void in
           make.top.equalTo(radio!.superview!).offset(10)
            make.left.equalTo(radio!.superview!).offset(30)
            make.right.equalTo((radio?.superview!)!).offset(-30)
            make.bottom.equalTo(radio!.superview!).offset(-10)
            make.center.equalTo((radio?.superview!)!)
        }
        
        
        你好。我用SnapKit设置了一个约束,但是还是显示出现了问题,是不是约束不对?没有起作用?

图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进