具有uitableviewdiffabledatasource的自定义单元格

问题描述

我有一个详细的ViewController,其像元是由这样的自定义uitableviewdiffabledata定义的:

{ (_,indexPath,item) -> UITableViewCell? in
            let color = UIColor(named: "blue")!
            if let _ = item as? TextFieldItem,let cell = tableView.dequeueReusableCell(withIdentifier: "textField",for: indexPath) as? TextFieldTableViewCell {
                cell.textField.text = recipe.wrappedValue.name
                cell.textField.placeholder = NSLocalizedString("name",comment: "")
                cell.selectionStyle = .none
                cell.textChanged = nameChanged
                cell.backgroundColor = color
                return cell
            } else if let imageItem = item as? ImageItem,let imageCell = tableView.dequeueReusableCell(withIdentifier: "image",for: indexPath) as? ImageTableViewCell {
                imageCell.setup(imageData: imageItem.imageData)
                return imageCell
            } else if let _ = item as? AmountItem,let amountCell = tableView.dequeueReusableCell(withIdentifier: "times",for: indexPath) as? AmountTableViewCell{
                amountCell.setUp(with: recipe.wrappedValue.timesText,format: formatAmount)
                amountCell.backgroundColor = color
                return amountCell
            } else if item is InfoItem {
                return  InfoTableViewCell(infoText: Binding(get: {
                    return recipe.wrappedValue.info
                },set: updateInfo),reuseIdentifier: "info")
            } else if let stripItem = item as? InfoStripItem,let infoStripCell = tableView.dequeueReusableCell(withIdentifier: "infoStrip",for: indexPath) as? InfoStripTableViewCell {
                infoStripCell.setUpCell(for: stripItem)
                return infoStripCell
            } else if let stepItem = item as? StepItem {
                let stepCell = StepTableViewCell(style: .default,reuseIdentifier: "step")
                stepCell.setUpCell(for: stepItem.step)
                return stepCell
            } else if let detailItem = item as? DetailItem,let cell = tableView.dequeueReusableCell(withIdentifier: "detail",for: indexPath) as? DetailTableViewCell {
                let title = NSAttributedString(string: detailItem.text,attributes: [.foregroundColor : UIColor.label])
                cell.textLabel?.attributedText = title
                cell.accessoryType = .disclosureIndicator
                cell.backgroundColor = color
                return cell
            }
            return UITableViewCell()
        }

并且我想制作如下定义的InfoTableViewCell:

class InfoTableViewCell: UITableViewCell {
    @Binding private var infoText: String
    
    private var textView = UITextView()

    init(infoText: Binding<String>,reuseIdentifier: String?) {
        self._infoText = infoText
        super.init(style: .default,reuseIdentifier: reuseIdentifier)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup() {
        self.addSubview(textView)
        textView.fillSuperview()
        
        
        textView.text = infoText
        textView.delegate = self
        
        textView.backgroundColor = UIColor(named: "blue")!
        textView.font = UIFont.preferredFont(forTextStyle: .body)
    }
    
}

extension InfoTableViewCell: UITextViewDelegate {
    
    func textViewDidChange(_ textView: UITextView) {
        self.infoText = textView.text
    }
    
}

,我想根据textField中的多数民众赞成在调整此单元格的大小。有关如何操作的任何提示? P. S.我正在使用LBTA工具用textField填充单元格的内容

解决方法

根据我的经验,只需省略tableView(_:heightForRowAt:)表视图委托。然后,在创建单元格时,创建将单元格顶部和底部绑定到内容顶部和底部的约束。然后该单元将增长或缩小以满足约束条件。

在自定义表格视图单元格类的init中:

textView.topAnchor.constraint(equalTo: self.topAnchor,constant: 8).isActive = true
textView.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: -8).isActive = true

不确定LBTA工具如何影响此行为,因此这可能仅适用于原始UIKit。