簡體   English   中英

動態Tableview單元格

[英]Dynamic Tableview cell

我想創建動態表格單元格視圖,可以根據標簽中的文本對齊元素。 單元格高度與標簽中的文本不同。 設計此類設計的最佳方法是什么? 如何在表格視圖單元格中獲得精確對齊,因為標簽文本在三行中,其他元素在中心水平對齊我不想截斷我想要全文的尾部。

如果文本是兩行樣本。

提前致謝。

對齊:

您可以創建與左標簽具有相同高度的正確標簽。 然后嘗試在中間對齊文本。 為此,您可以繼承UILabel類並將文本rect對齊更改為middle。

For Dynamic Height:
 1. Use auto layout when creating your table view cells.
 2. Set the table view rowHeight to equal UITableViewAutomaticDimension.
 3. Set the estimatedRowHeight or implement the height estimation delegate method.

    Pro Tip: the trick to getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.
    Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.
    The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.
    If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.


For Detail, please refer to original source of above text is https://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

首先,您可以在viewDidLoad中設置示例:

tableView.estimatedRowHeight = 160
tableView.rowHeight = UITableViewAutomaticDimension

然后,當您繪制xib單元格時,可以遵循以下模式:

+-view----------------+
|       |             |
|    -label-          |
|       |             | 
| +-subview--------+  |
| |      |   |     |  | 
|-| -label-label-  |--|
| |      |   |     |  | 
| |   -label-      |  | 
| |      |         |  |
| +----------------+  |
|        |            |
|     -label-         |
|        |            | 
+---------------------+

其中每個標簽都設置了頂部,底部,前導和尾隨約束。 如果您需要壓縮標簽組,可以添加子視圖以將其包含在其中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM