如何让多个UILabel在UITableView Cell中自我调整大小?

时间:2015-11-04 12:17:46

标签: ios xcode swift uitableview uilabel

我在Xcode中有一个TableViewController Swift项目。 我制作了3个标签:titleLabeldeadlineLabelnoteLabel

titleLabel为空时,如何让deadlineLabelnoteLabel自行调整大小并更改其在单元格中的位置?

或者当titleLabeldeadLineLabel出现时,如何让noteLabel自我调整大小并改变其位置?

提前谢谢!

以下是我的一些代码:

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!

1 个答案:

答案 0 :(得分:1)

你做不到。

或者学习它:您可以根据内容的大小自动重新设置单元格视图,但仅此一点对您没有多大帮助。

您必须相应地回复tableView(_:heightForRowAtIndexPath:)的来电。 因此,覆盖UITableViewDelegate协议的此方法,在那里进行大小计算并相应地返回其高度。

相关问题