ios - 标签从上到下有额外的空间吗?

标签 ios swift xcode autolayout

我在下面创建了一个自定义单元格,如下所示。 enter image description here

问题是当我第一次启动时,它需要高度(从顶部和底部开始)。但是当向上和向下滚动时,额外的高度会被删除。我无法理解为什么会发生这个问题。

顶部标签的行号是固定的。 第二个标签的行号为 0。 enter image description here

Controller 代码

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    
    return UITableViewAutomaticDimension
}

func numberOfSections(in tableView: UITableView) -> Int {
    
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:SampleCell = tableView.dequeueReusableCell(withIdentifier: "SampleCell") as! SampleCell
    if indexPath.row == 0 {
        cell.labelDescription.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.f Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.f Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.f Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like "
        
    } else {
        cell.labelDescription.text = "skdfjksj gkfjg"
    }
    tableView.sizeToFit()
    return cell
}

UIImage 的约束

enter image description here

顶级 UILabel 的约束 enter image description here

底部 UILabel 的约束 enter image description here

最佳答案

UITableViewAutomaticDimension设置为heightForRowAt

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return UITableViewAutomaticDimension  
}

UILabelnumberOfLine设置为0 & 设置前导尾随顶部底部约束

关于ios - 标签从上到下有额外的空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637638/

相关文章:

ios - 迁移restkit v0.20

iPhone 的 C++ 编译(STL 问题?)

objective-c - 如何使用 NSDateFormater 从 NSString 转换为 NSDate

ios - Facebook LoginManager 回调/PrepareforSegue Swift

ios - Crashlytics 手动更新到 3.8.4 导致链接器错误

swift - 在闭包中使用无主引用

ios - 在 appUser 滚动所有屏幕后跳过入职屏幕?

ios - React Native/Swift Native 模块只能工作一次

iphone - 如何检测 iPhone 中的轻微运动抖动?

objective-c - cocoa 中少量数据的数据持久化