ios - UITableViewCell自定义高度

标签 ios swift uitableview autolayout swift2

我正在尝试为我的 UITableViewCell 设置自定义高度。在 Xcode 中,我给了它一个自定义高度并添加了一个 UImageView subview ,它看起来不错。然而,在运行时,单元格的高度会回落到默认值,并且我的图像被严重裁剪。

我已经将委托(delegate)设置为使用动态高度,并且在 Xcode 中为单元格提供了自定义高度,并且我的图像设置为宽高比填充。我不确定我还缺少什么。

这是我的VC:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch indexPath.section {
    case 0:
        return UITableViewAutomaticDimension
    default: ()
    return 50
    }
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch indexPath.section {
    case 0:
        return 300
    default: ()
    return 50
    }
}

最佳答案

当然,这不是一个很好且清晰的答案,但设置自动布局可能存在问题。通常很难在这里提供所有数据的布局和约束,因此您最好检查一些 Material ,例如 this 。然后检查您的代码。

关于ios - UITableViewCell自定义高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115464/

相关文章:

iOS MagicalRecord 表格 View 的分组和排序。它们究竟是如何工作的?

ios - 如何在没有 Storyboard的情况下制作滑出菜单?

swift - 快速访问默认情况下的开关案例参数

swift - 将字符串转换为 cllocationcoordinate2d swift 数组

iOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView

ios - UITableView 行数像 iOS 邮件应用

android - 移动应用程序安装量和每日活跃用户数 (DAU) 之间的差异

ios - 深色模式不会立即更新

Swift 可选的 nil 检查返回与实际情况不同的结果

ios - 使用 NSFetchedResultsController 在 TableView 上显示的元素列表与底层 CoreData 实体不匹配