ios - 如何使多个 UILabel 在 UITableView 单元格中自行调整大小?

标签 ios xcode swift uitableview uilabel

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

如何让 titleLabeldeadlineLabelnoteLabel 为空时自动调整大小并改变它们在单元格中的位置?

或者当 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!

最佳答案

你不能那样做。

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

您必须相应地响应 tableView(_:heightForRowAtIndexPath:) 的调用。 因此,覆盖 UITableViewDelegate 协议(protocol)的这个方法,在那里进行尺寸计算并相应地返回它的高度。

关于ios - 如何使多个 UILabel 在 UITableView 单元格中自行调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521720/

相关文章:

swift - 添加带有图像的 UINavigationbar 按钮时出错

linux - 完美/快速编译在 OSX 中工作,在 Linux 中失败

swift - 登录或注册分段控制按钮

ios - 无法识别的选择器发送到实例,将按钮连接到 View Controller 后出现 Storyboard 错误

ios - 子类化静态库中的类

xcode - 无法验证 Mapbox.framework 中的位码

ios - swift 3 : Decimal to Int

ios - 我的应用程序中缺少 iAd

ios - 使用NSDictionary进行JSON解析并按键查找值

ios - 带有 iOS 14 设备的 Xcode 10 - 尝试与此设备通信时遇到错误。 (该服务无效。)