Is there possible way to adjust table view width by width of biggest text that should be set in the cellTitle label?
是否有可能通过cellTitle标签中设置的最大文本宽度调整表视图宽度?
iOS 8 , SWIFT
iOS 8,SWIFT
4 个解决方案
#1
- Set a
width
auto-layout constraint to the table view. - Set an
outlet
from the constraint to your code (ctrl+drag, just like a view). -
When you have the size of the label (if the label also has auto-layout then you will have it at
viewDidLayoutSubviews
) set the width to the table like so:如果你有标签的大小(如果标签也有自动布局,那么你将在viewDidLayoutSubviews上)将宽度设置为表,如下所示:
- (void)viewDidLayoutSubviews { self.myTableWidthConstraint.constant = self.myLabel.frame.size.width; [self.myTableView updateConstraints]; [self.myTableView layoutIfNeeded]; }
将宽度自动布局约束设置为表视图。
将约束的出口设置为代码(ctrl + drag,就像视图一样)。
#2
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Set the popover tableview width to be max compressed size (assumes all cells visible)
let maxWidth = tableView.visibleCells.reduce(0.0) { (currentMax, cell) -> CGFloat in
return max(currentMax, cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).width)
}
var contentSize = tableView.contentSize
contentSize.width = maxWidth
tableView.contentSize = contentSize
tableView.bounds.size = contentSize
}
#3
You can achieve it by writing 2 lines in your viewDidLoad
:
您可以通过在viewDidLoad中写入2行来实现它:
//what ever you want the estimated row height
self.tableView.estimatedRowHeight = 280
self.tableView.rowHeight = UITableViewAutomaticDimension
Note : its only applied for IOS 8
注意:它仅适用于IOS 8
#4
You should be able to do that in the size inspector.
您应该能够在尺寸检查器中执行此操作。
#1
- Set a
width
auto-layout constraint to the table view. - Set an
outlet
from the constraint to your code (ctrl+drag, just like a view). -
When you have the size of the label (if the label also has auto-layout then you will have it at
viewDidLayoutSubviews
) set the width to the table like so:如果你有标签的大小(如果标签也有自动布局,那么你将在viewDidLayoutSubviews上)将宽度设置为表,如下所示:
- (void)viewDidLayoutSubviews { self.myTableWidthConstraint.constant = self.myLabel.frame.size.width; [self.myTableView updateConstraints]; [self.myTableView layoutIfNeeded]; }
将宽度自动布局约束设置为表视图。
将约束的出口设置为代码(ctrl + drag,就像视图一样)。
#2
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Set the popover tableview width to be max compressed size (assumes all cells visible)
let maxWidth = tableView.visibleCells.reduce(0.0) { (currentMax, cell) -> CGFloat in
return max(currentMax, cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).width)
}
var contentSize = tableView.contentSize
contentSize.width = maxWidth
tableView.contentSize = contentSize
tableView.bounds.size = contentSize
}
#3
You can achieve it by writing 2 lines in your viewDidLoad
:
您可以通过在viewDidLoad中写入2行来实现它:
//what ever you want the estimated row height
self.tableView.estimatedRowHeight = 280
self.tableView.rowHeight = UITableViewAutomaticDimension
Note : its only applied for IOS 8
注意:它仅适用于IOS 8
#4
You should be able to do that in the size inspector.
您应该能够在尺寸检查器中执行此操作。