Ios 根据内容以编程方式自动调整UITableViewCell的大小
我正在创建一个模拟与真实用户聊天的应用程序,对于聊天日志布局,我使用的是Ios 根据内容以编程方式自动调整UITableViewCell的大小,ios,swift,uitableview,Ios,Swift,Uitableview,我正在创建一个模拟与真实用户聊天的应用程序,对于聊天日志布局,我使用的是UITableView,因为我对Swift非常陌生,但习惯于使用其他语言编程,所以我以编程方式创建整个布局 当前我的ViewController如下所示 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var navigationBar: UINavigationB
UITableView
,因为我对Swift非常陌生,但习惯于使用其他语言编程,所以我以编程方式创建整个布局
当前我的ViewController
如下所示
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var navigationBar: UINavigationBar!
@IBOutlet weak var tableView: UITableView!
let rows: [String] = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam euismod nunc id nulla ultricies tempus. Mauris a euismod nunc."
]
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AiMessageCell") as! AiMessageCell
// cell.textLabel?.text = rows[indexPath.row]
// cell.textLabel?.numberOfLines = 0;
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// tableView.beginUpdates()
// tableView.endUpdates()
return UITableViewAutomaticDimension;
}
override func viewDidLoad() {
super.viewDidLoad()
// tableView.estimatedRowHeight = 140
// tableView.rowHeight = UITableViewAutomaticDimension;
tableView.register(AiMessageCell.self, forCellReuseIdentifier: "AiMessageCell")
tableView.register(UserMessageCell.self, forCellReuseIdentifier: "UserMessageCell")
tableView.register(UserSelectionCell.self, forCellReuseIdentifier: "UserSelectionCell")
}
}
它只需要一行,并基于它(当前未将文本传递到单元格)将一行输出到myUITableView
我有AiMessageCell
逻辑
class AiMessageCell: UITableViewCell, NSLayoutManagerDelegate {
// @IBOutlet weak var wrapper: UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let wrapper = UIView(
frame: CGRect(
x: 10, y: 10,
width: UIScreen.main.bounds.width - 10 - 10,
height: 45
)
)
let balloon = UIView(
frame: CGRect(
x: 40, y: 0,
width: 80,
height: 45.5
)
)
balloon.layer.cornerRadius = 10;
balloon.backgroundColor = UIColor.init(
red: CGFloat(240/255.0),
green: CGFloat(240/255.0),
blue: CGFloat(240/255.0),
alpha: CGFloat(1.0)
);
let message = UITextView(
frame: CGRect(
x: 10, y: 5,
width: balloon.layer.bounds.width - 10,
height: balloon.layer.bounds.height - 10
)
);
message.text = "What is shown in the scrollable content area of a scroll view is determined by its subviews. You need check constraint in your storyboard. It's seems like you're forget constraint from bottom text block to bottom cell. And then uncomment this
tableView.estimatedRowHeight = 140
class AimMessageCell:UITableViewCell,NSLayoutManagerDelegate{
//@ibvar包装器:UIView!
重写初始化(样式:UITableViewCellStyle,reuseIdentifier:String?){
init(样式:style,reuseIdentifier:reuseIdentifier)
让wrapper=UIView(
帧:CGRect(
x:10,y:10,
宽度:UIScreen.main.bounds.width-10-10,
身高:45
)
)
让balloon=UIView(
帧:CGRect(
x:40,y:0,
宽度:80,
身高:45.5
)
)
balloon.layer.cornerRadius=10;
balloon.backgroundColor=UIColor.init(
红色:CGFloat(240/255.0),
绿色:CGFloat(240/255.0),
蓝色:CGFloat(240/255.0),
alpha:CGFloat(1.0)
);
let message=UITextView(
帧:CGRect(
x:10,y:5,
宽度:balloon.layer.bounds.width-10,
高度:balloon.layer.bounds.height-10
)
);
message.text=“滚动视图的可滚动内容区域中显示的内容由其子视图决定。您需要在故事板中检查约束。似乎您忘记了从底部文本块到底部单元格的约束。然后取消对该约束的注释
tableView.estimatedRowHeight = 140
tableView.rowHeight = UITableViewAutomaticDimension
您需要在故事板中检查约束。似乎您忘记了从底部文本块到底部单元格的约束。然后取消对该约束的注释
tableView.estimatedRowHeight = 140
tableView.rowHeight = UITableViewAutomaticDimension
我认为这里的问题是,您使用的是框架而不是布局约束来定义单元格子视图的大小。当您使用UITableViewAutomaticDimension
时,您需要正确设置UITableViewCell顶部和底部的约束
我认为它比您在viewDidLoad
中的注释更简洁,而不是使用委托:
我认为这里的问题是,您使用的是框架而不是布局约束来定义单元格子视图的大小。当您使用UITableViewAutomaticDimension
时,您需要正确设置UITableViewCell顶部和底部的约束
我认为它比您在viewDidLoad
中的注释更简洁,而不是使用委托: