【问题标题】:How to make multiple UILabels to self resize in a UITableView Cell?如何使多个 UILabel 在 UITableView 单元中自行调整大小?
【发布时间】:2015-11-04 12:17:45
【问题描述】:

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

noteLabel 为空时,如何使titleLabeldeadlineLabel 自行调整大小并更改它们在单元格中的位置?

或者当deadLineLabelnoteLabel 都没有出现时,如何让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!

【问题讨论】:

    标签: ios xcode swift uitableview uilabel


    【解决方案1】:

    你不能那样做。

    或者是学术性的:您可以让单元格视图根据其内容的大小自动调整大小,但仅此一项对您没有多大帮助。

    您必须相应地响应tableView(_:heightForRowAtIndexPath:) 的呼叫。 所以覆盖UITableViewDelegate协议的这个方法并在那里进行大小计算并相应地返回它的高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      相关资源
      最近更新 更多