ios - 如何在快速的集合视图单元格中输入时调整 UITextView 的大小
问题描述
我是 swift 新手,无法调整集合视图单元格中 textview 的大小。我需要在编辑时同时调整 textview 的高度和集合视图单元格的高度。我的代码是这样的
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!
}
}
我需要调整文本视图和集合视图单元格的大小。有可能吗?如果可能,请分享一些代码。提前致谢!
解决方案
推荐阅读
- django - Django 找不到存在于 postgreSQL 数据库中的表
- asp.net-core - 您可以在两种查询类型之间创建一对一的关系吗?
- python - 数值积分 Python 与 Matlab
- python - Keras:model.fit() 和 model.fit_generator() 返回历史对象。如何获得 Keras 模型?
- docker - 如何找到主机中的 docker 容器目录在哪里?
- pandas - 遍历数据框列以查看单个单元格值是否在文件路径列表中
- rabbitmq - 我想向 RabbitMQ 中的特定消费者发送消息。你有解决方案吗?
- vim - Vim打开一定大小但关闭时返回终端窗口大小
- swift - 转换 String.Index?到国际
- python-3.x - Python 3.5:将字符串作为参数传递给子进程过程