Как изменить размер UITextView при вводе внутри него в ячейке представления коллекции в Swift - PullRequest
0 голосов
/ 05 августа 2020

Я новичок в Swift, и я не могу изменить размер текстового представления в ячейке представления коллекции. Мне нужно изменить размер высоты текстового представления, а также высоту ячейки представления коллекции одновременно во время редактирования. Мой код выглядит так:

class TimeSheetCollectionViewCell1: UICollectionViewCell {
    @IBOutlet var txtviewTimeSheet: UITextView!
    @IBOutlet var btnDelete: UIButton!
    @IBOutlet var txtviewTimeSheetHeight: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        txtviewTimeSheet.layer.borderWidth = 0.5
        txtviewTimeSheet.layer.borderColor = #colorLiteral(red: 0.7810397744, green: 0.7810582519, blue: 0.7810482979, alpha: 1)
        txtviewTimeSheet.layer.cornerRadius = 8
        txtviewTimeSheet.autocorrectionType = .no
        let iconUniChar: UniChar = 0xf057
        btnDelete.setTitle(String(format: "%C", iconUniChar), for: .normal)
        btnDelete.setTitleColor(.red, for: .normal)
    }
}

extension TimeSheetViewController:UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return arrcheck.count
    }
    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TimeSheetCollectionViewCell1", for: indexPath as IndexPath) as! TimeSheetCollectionViewCell1
        cell.txtviewTimeSheet.text = arrcheck[indexPath.row]
        cell.txtviewTimeSheet.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(doneButtonClicked))
        cell.btnDelete.tag = indexPath.item
        cell.btnDelete.addTarget(self, action: #selector(btnDeleteClick), for: .touchUpInside)
        
        cell.txtviewTimeSheet.delegate = self
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let label = UITextView(frame: CGRect.zero)
        label.text = arrcheck[indexPath.item]
        label.sizeToFit()
        return CGSize(width: (collectionView.frame.size.width/1) - 10, height: label.frame.height)
    }
    
    @objc func btnDeleteClick(_ sender: UIButton)
    {
        print("buttonPressed ! \(sender.tag)")
        arrcheck.remove(at: sender.tag)
        CollectionViewTimeSheet.reloadData()
    }
    @objc func doneButtonClicked(_ sender: Any) {
        print("Done")
        print("Text Field = ",textFieldIndexPath)
        print("Text Filed Row = ",textFieldIndexPath.row)
        print("Text Filed Section = ",textFieldIndexPath.section)
        print(arrcheck[textFieldIndexPath.row])
        print("Arr = ",arrcheck)
        CollectionViewTimeSheet.reloadData()
    }
}

class TimeSheetViewController: UIViewController ,UITextViewDelegate{
func textViewDidEndEditing(_ textView: UITextView) {
        let cell: UICollectionViewCell = textView.superview?.superview as! UICollectionViewCell
        let table: UICollectionView = cell.superview as! UICollectionView
        textFieldIndexPath = table.indexPath(for: cell)!
        print("Text Field = ",textFieldIndexPath)
        print(textView.text as Any)
        arrcheck[textFieldIndexPath.row] = textView.text!
    }
}

Мне нужно изменить размер текстового представления и ячейки представления коллекции. Возможно ли это? Если возможно, поделитесь кодом. Заранее спасибо!

введите описание изображения здесь

...