在表视图单元格内隐藏按钮

时间:2018-04-24 02:47:28

标签: ios swift uitableview

隐藏UITableViewCell中的按钮时遇到问题。如果label.text比标签容器大,我将向UITableViewCell添加一个按钮,因此如果文本被截断。我遇到了问题或错误,即使文本没有被截断,按钮也会出现。这就是模拟器的样子。

enter image description here

因此,当文本未截断时,按钮仍会显示。我正在尝试使用控制台来弄清楚发生了什么。这很奇怪,因为控制台正确地打印出来,但当我开始滚动时,它会显示标签被截断。以下是当应用程序首次在左侧启动时控制台打印出来的内容,这是正确的。当我开始滚动时,它打印出右边的那个,Jackie应该是假的而不是真的。

enter image description here

现在我不知道为什么它应该是假的时打印出来的。我不确定这是否发生,因为我们重用UITableViewCell。但这就是我的UITableViewCell文件的样子。另外isTruncated是一个帮助方法,我发现它可以帮助我检查标签是否被截断。

class ItemCell: UITableViewCell {

@IBOutlet weak var itemImageView: UIImageView!
@IBOutlet weak var itemTitleLabel: UILabel!
@IBOutlet weak var itemDetailLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!

weak var delegate: ItemCellDelegate?

var item: Item? {
    didSet {

        configureCell()
    }
}

private func configureCell() {

    guard let item = item else { return }

    itemDetailLabel.text = item.description
    itemImageView.image = item.image
    itemTitleLabel.text = item.name

    if itemDetailLabel.isTruncated {
        moreButton.isHidden = false
    }
    else {
        moreButton.isHidden = true
    }

    print("Name: \(item.name)\nisTextTruncated: \(itemDetailLabel.isTruncated)\n")
}

@IBAction func showMoreBtn() {

    delegate?.getSelected(cell: self)
}
}

UITableView cellForRowAt方法

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as? ItemCell else {
        return UITableViewCell()
    }

    let item = categories[indexPath.section].items[indexPath.row]


    cell.item = item

    cell.delegate = self

    //Even tried updating the data model
    // categories[indexPath.Section].items[indexPath.row] = item

    return cell
}

一直在这里试图弄清楚发生了什么,所以我真的很感激帮助。

好的,这是isTruncated代码。这里也是我刚发布的视频的链接,以便更好地了解问题。 Video Sample,注意Jackie单元格/行。

extension UILabel {

var isTruncated: Bool {

    guard let labelText = text else {
        return false
    }

    let labelTextSize = (labelText as NSString).boundingRect(
        with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
        options: .usesLineFragmentOrigin,
        attributes: [.font: font],
        context: nil).size

    return labelTextSize.height > bounds.size.height
}
}

2 个答案:

答案 0 :(得分:1)

我认为问题在于获得isTruncated的价值。尝试使用此扩展名来检查文本是否被截断。我认为行号检查将是这样做的诀窍。

extension UILabel {

    func countLabelLines() -> Int {
        // Call self.layoutIfNeeded() if your view is uses auto layout
        let myText = self.text! as NSString
        let attributes = [NSFontAttributeName : self.font]

        let labelSize = myText.boundingRect(with: CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
        return Int(ceil(CGFloat(labelSize.height) / self.font.lineHeight))
    }

    func isTruncated() -> Bool {

        if (self.countLabelLines() > self.numberOfLines) {
            return true
        }
        return false
    }
} 

答案 1 :(得分:0)

尝试下面的代码来创建reusableCell。

let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell") as! ItemCell

为?  它将返回一个可选值,如果向下转换是不可能的,它将返回nil,在你的情况下,因为guard语句它返回UITableViewCell