Ios 两个标签在tableView单元格中垂直对齐自动调整大小
尝试自动调整单元格内垂直对齐的两个标签的大小,可能吗 在Ios 两个标签在tableView单元格中垂直对齐自动调整大小,ios,uitableview,autolayout,Ios,Uitableview,Autolayout,尝试自动调整单元格内垂直对齐的两个标签的大小,可能吗 在情节提要中,我们必须设置自动布局,因为我想自动调整两个标签的大小(一个在另一个上) 我无法设置每个高度,因此故事板不知道这两个标签的高度,因此它会显示自动布局的错误 如果我单击“添加缺少的约束”按钮,它将为“副标题”添加一个高度 或者我可以为“标题”而不是“副标题”设置高度 或者使“标题”等于“副标题”,它仍然可以接受 结果如下: 解决方法将这两个单元分离到另一个单元。 有更好的解决办法吗?还是我只是忘了做什么 另外,我在iOS 10
情节提要
中,我们必须设置自动布局
,因为我想自动调整两个标签的大小(一个在另一个上)
我无法设置每个高度,因此故事板不知道这两个标签的高度,因此它会显示自动布局的错误
如果我单击“添加缺少的约束”按钮,它将为“副标题”添加一个高度
或者我可以为“标题”而不是“副标题”设置高度
或者使“标题”等于“副标题”,它仍然可以接受
结果如下:
解决方法将这两个单元分离到另一个单元。
有更好的解决办法吗?还是我只是忘了做什么
另外,我在iOS 10.3上使用Xcode 8 尝试为两个标签设置适当的约束-:
情节提要-:
class ViewController1: UIViewController {
var dataForTableView:[ProductImage]?
@IBOutlet weak var secondTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
secondTable.estimatedRowHeight = 96
secondTable.rowHeight = UITableViewAutomaticDimension
// CHECK FOR DATA
//print(dataForTableView?[0].url as Any)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController1 : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! testingCell2
cell.backgroundColor = UIColor.blue;
cell.secondLabel.text = "agfhjsgfhjsgdshgfhjsfjhvhssajs hjfbvhjfbvjhfgafgfhlgkaghkfakfhkflbvhfbvhfvbhfv ah fvfhbvfjhvbfhdavhfvhv"
return cell
}
// Number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension
}
}
第一个标签的约束条件
-:
第二个标签的约束条件
-:
控制器类(对表视图使用自动尺寸标注)-:
class ViewController1: UIViewController {
var dataForTableView:[ProductImage]?
@IBOutlet weak var secondTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
secondTable.estimatedRowHeight = 96
secondTable.rowHeight = UITableViewAutomaticDimension
// CHECK FOR DATA
//print(dataForTableView?[0].url as Any)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController1 : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! testingCell2
cell.backgroundColor = UIColor.blue;
cell.secondLabel.text = "agfhjsgfhjsgdshgfhjsfjhvhssajs hjfbvhjfbvjhfgafgfhlgkaghkfakfhkflbvhfbvhfvbhfv ah fvfhbvfjhvbfhdavhfvhv"
return cell
}
// Number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension
}
}
输出-:
class ViewController1: UIViewController {
var dataForTableView:[ProductImage]?
@IBOutlet weak var secondTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
secondTable.estimatedRowHeight = 96
secondTable.rowHeight = UITableViewAutomaticDimension
// CHECK FOR DATA
//print(dataForTableView?[0].url as Any)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController1 : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! testingCell2
cell.backgroundColor = UIColor.blue;
cell.secondLabel.text = "agfhjsgfhjsgdshgfhjsfjhvhssajs hjfbvhjfbvjhfgafgfhlgkaghkfakfhkflbvhfbvhfvbhfv ah fvfhbvfjhvbfhdavhfvhv"
return cell
}
// Number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension
}
}
我希望这就是你想要的,如果有任何问题,请告诉我。谢谢你可以设置高度并使其成为>=。