我們可以將左右兩側的多行標簽設定為影像嗎?請檢查所附圖片。
是的,我正在使用 tableview 單元格 xib。需要在約束條件下執行此操作。

我嘗試了左影像視圖 - 容器中的前導、寬度、高度、垂直
第二個標簽 - 頂部,底部,導致第一個影像,尾隨到第二個影像。行數 = 0。
與Uiview嵌入的右影像視圖 - 這是在選擇影像背景顏色更改為藍色的藍色影像約束 - 尾部,寬度,高度,垂直在容器中
問題是存在大文本時標簽會被截斷。
之后,我嘗試洗掉第二張圖片的寬度,但遇到了約束問題。
請幫我解決這個問題。
先感謝您。


uj5u.com熱心網友回復:
由于多種原因,可以阻止單元格執行動態自動調整大小:
約束可能會阻止動態高度:
要確認約束,請在除錯器中運行應用程式,然后點擊“Debug View Hierarchy”按鈕并確認約束。這是查看所有約束的快速方法(并確保沒有任何您不想要的約束):

如果您有一些無關緊要的約束(例如,與頂部/底部相結合的意外高度),則可能會導致標簽的高度無法適應文本。
可以將表格視圖定義為不支持單元格的自動尺寸:
問題的另一個可能來源是表格視圖的行高設定為某個固定值,從而阻止標簽擴展以適應文本。確認表格視圖的行高設定為
automaticDimension。在 IB 中:
或者,以編程方式:
override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell") tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 44 }您可以在代碼中包含一些強制特定單元格高度的內容:
不用說,請確保您沒有在

并不是說它是相關的,但這是生成上述影像的代碼。誠然,這里沒什么可看的:
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var strings = [
"This is a very long string that goes on and on. This is so long that it will take a few lines. This one takes four lines, but the next one takes only one.",
"This is short string.",
"This is a very long string that goes on and on."
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return strings.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.customLabel.text = strings[indexPath.row]
return cell
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/517938.html
標籤:IOS迅速
