我在 Xcode 中有一个 TableViewController
Swift 项目。
我制作了 3 个标签:titleLabel
、deadlineLabel
和 noteLabel
。
如何让 titleLabel
和 deadlineLabel
在 noteLabel
为空时自动调整大小并改变它们在单元格中的位置?
或者当 deadLineLabel
和 noteLabel
都没有出现时,如何让 titleLabel
自动调整大小和改变它的位置?
提前致谢!
下面是我的一些代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCellWithIdentifier("todoCell", forIndexPath: indexPath) // retrieve the prototype cell (subtitle style)
let cell = tableView.dequeueReusableCellWithIdentifier("todoCell", forIndexPath: indexPath) as! ToDoTableViewCell
let todoItem = todoItems[indexPath.row] as ToDoItem
cell.titleLabel.text = todoItem.title as String!
if (todoItem.isOverdue) { // the current time is later than the to-do item's deadline
cell.deadlineLabel.textColor = UIColor.redColor()
} else {
cell.deadlineLabel.textColor = UIColor.blueColor() // we need to reset this because a cell with red subtitle may be returned by dequeueReusableCellWithIdentifier:indexPath:
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "'Due' MMM dd 'at' h:mm a" // example: "Due Jan 01 at 12:00 PM"
cell.deadlineLabel.text = dateFormatter.stringFromDate(todoItem.deadline)
cell.noteLabel.text = todoItem.note as String!
最佳答案
你不能那样做。
或者说是学术性的:您可以让单元格 View 根据其内容的大小自动重新调整,但仅此一项对您没有多大帮助。
您必须相应地响应 tableView(_:heightForRowAtIndexPath:)
的调用。
因此,覆盖 UITableViewDelegate
协议(protocol)的这个方法,在那里进行尺寸计算并相应地返回它的高度。
关于ios - 如何使多个 UILabel 在 UITableView 单元格中自行调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521720/