ios - UITableViewCell 高度不正确,sizeToFit 尺寸不正确

标签 ios iphone swift uitableview

我正在尝试创建自定义 UITableViewCell,但遇到单元格框架高度适当的问题。这很令人不安,因为运行 iOS 8.4 的 iPhone 4s/5s 的单元格大小正确,但运行相同操作系统的 iPhone 6/6+ 则不然。

messageLabel 上调用 sizeToFit 时会出现困惑。有些标签几乎看起来下面有额外的空白行,但显然没有单元格显示的那么高。

下面是自定义单元格。导致问题的标签是 messageLabel。要查看标签的框架,let border = true

//
//  NotesTableViewCell.swift
//  urchin
//
//  Created by Ethan Look on 6/17/15.
//  Copyright (c) 2015 Tidepool. All rights reserved.
//

import Foundation
import UIKit

let noteCellHeight: CGFloat = 128
let noteCellInset: CGFloat = 16
let labelSpacing: CGFloat = 6

class NoteCell: UITableViewCell {

    let borders = false

    var cellHeight: CGFloat = CGFloat()

    let usernameLabel: UILabel = UILabel()
    let timedateLabel: UILabel = UILabel()
    var messageLabel: UILabel = UILabel()

    func configureWithNote(note: Note) {

        usernameLabel.text = note.user!.fullName
        usernameLabel.font = UIFont(name: "OpenSans-Bold", size: 17.5)!
        usernameLabel.textColor = UIColor.blackColor()
        usernameLabel.sizeToFit()
        let usernameX = noteCellInset
        let usernameY = noteCellInset
        usernameLabel.frame.origin = CGPoint(x: usernameX, y: usernameY)

        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "EEEE M.d.yy h:mm a"
        var dateString = dateFormatter.stringFromDate(note.timestamp)
        dateString = dateString.stringByReplacingOccurrencesOfString("PM", withString: "pm", options: NSStringCompareOptions.LiteralSearch, range: nil)
        dateString = dateString.stringByReplacingOccurrencesOfString("AM", withString: "am", options: NSStringCompareOptions.LiteralSearch, range: nil)
        timedateLabel.text = dateString
        timedateLabel.font = UIFont(name: "OpenSans", size: 12.5)!
        timedateLabel.textColor = UIColor.blackColor()
        timedateLabel.sizeToFit()
        let timedateX = contentView.frame.width - (noteCellInset + timedateLabel.frame.width)
        let timedateY = usernameLabel.frame.midY - timedateLabel.frame.height / 2
        timedateLabel.frame.origin = CGPoint(x: timedateX, y: timedateY)

        messageLabel.frame.size = CGSize(width: contentView.frame.width - 2 * noteCellInset, height: CGFloat.max)
        let hashtagBolder = HashtagBolder()
        let attributedText = hashtagBolder.boldHashtags(note.messagetext)
        messageLabel.attributedText = attributedText
        messageLabel.adjustsFontSizeToFitWidth = false
        messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        messageLabel.numberOfLines = 0
        messageLabel.sizeToFit()
        let messageX = noteCellInset
        let messageY = usernameLabel.frame.maxY + 2 * labelSpacing
        messageLabel.frame.origin = CGPoint(x: messageX, y: messageY)

        contentView.addSubview(usernameLabel)
        contentView.addSubview(timedateLabel)
        contentView.addSubview(messageLabel)

        cellHeight = noteCellInset + usernameLabel.frame.height + 2 * labelSpacing + messageLabel.frame.height + noteCellInset

        if (borders) {
            usernameLabel.layer.borderWidth = 1
            usernameLabel.layer.borderColor = UIColor.redColor().CGColor

            timedateLabel.layer.borderWidth = 1
            timedateLabel.layer.borderColor = UIColor.redColor().CGColor

            messageLabel.layer.borderWidth = 1
            messageLabel.layer.borderColor = UIColor.redColor().CGColor

            self.contentView.layer.borderWidth = 1
            self.contentView.layer.borderColor = UIColor.blueColor().CGColor
        }

        self.contentView.frame.size = CGSize(width: self.contentView.frame.width, height: cellHeight)
    }  
}

heightForRowAtIndexPath:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       let cell = NoteCell(style: .Default, reuseIdentifier: nil)
       cell.configureWithNote(notes[indexPath.row])
       return cell.cellHeight
}

该项目是开源的并且 on Github ,所以请随意克隆存储库并亲自检查所有代码。

谢谢!

最佳答案

不幸的是,您不能这样做,因为首先调用 tableView(_:heightForRowAtIndexPath) 并且您返回的值用于创建您将在 tableView 中出队的单元格(_:cellForRowAtIndexPath)。单元格无法设置自己的大小,因为当它可以这样做时(例如 awakeFromNibprepareForResuse),表格 View 已经有一个高度值。我使用过一些奇怪的解决方法,但使用自动调整大小的表格 View 单元格会更容易。

检查一下: http://www.appcoda.com/self-sizing-cells/

关于ios - UITableViewCell 高度不正确,sizeToFit 尺寸不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352089/

相关文章:

ios - 如何永久关闭 iOS 模拟器 (React Native)

ios - UIButton Outlet 设置标题 Swift

iphone - 如何使用uinavigationcontroller

iphone - 无法将应用提交到App Store,无法选择任何应用

swift - 将按钮操作重定向到 Swift 中的另一个函数

ios - UIImageView 由于内存警告而崩溃,尝试加载大量图像

iOS 11+ 如何将现有核心数据迁移到共享应用组以用于扩展?

swift - 按下时隐藏底栏 - Iphone X 上的动画卡住

ios - 如何以编程方式获取有关 iphone 的设备数据提供程序(如 Verizon/AT&T)的详细信息?

iOS:不在已编译的应用程序中包含 Storyboard 字符串