我在UICollectionViewCell中有一些UITextField,我想知道其中的哪个正在被修改。每个元素都是以编程方式制作的。 UICollectionViewCell有一个swift类。该单元在UICollectionView内重复大约5次,因此我还需要将UITextField放在哪个单元中。
有任何建议吗?
首先,在cellForItemAt
中,您需要为将在该单元格中显示的textView
分配标签。用单元格的行号标识它是最容易的。例如:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.textView.tag = indexPath.row
}
接下来,在textViewDidChange
中,您输入了一个与要修改的textView的标签相对应的switch语句。在每种情况下,您都可以知道您要修改的textView,执行所需的任何代码。例如:
func textViewDidChange(_ textView: UITextView) {
switch textView.tag {
case 0:
// Code for first text view
break
case 1:
// Code for second text view
break
case 2:
// Code for third text view
break
case 3:
// Code for fourth text view
break
default:
// Code for fifth text view
break
}
}