简体   繁体   中英

UICollectionView with dynamic Section and Row iOS Swift 3.

I have collectionView in iOS Swift 3. In which 5 section and 5 row currently. this will be dynamic section and row. In each row and section i have textfield . My question is how to i detect textfield in textfield action func textFieldDidChange(textfieldChange: UITextField){}. i need to know textfield tag with section and row position. so that i can store textfield value into single Array. As i detect textfield tag of each section from 0 to 4 index. but textfield tag change in each section and textfield tag initialise 0 to 4 in each section. Here my textfield action function

func textFieldDidChange(textfieldChange: UITextField){
      }

Set button tag like this in cellForRow

let tag = indexPath.section*100 + indexPath.row
btnABC.tag = tag

Get row and section of the particular button on button action

let tag = sender.tag // make sure sender is button type.
let Row = tag % 100
let Section = (tag - Row)/100

You can get the section and Row value of the CollectionView's Item in which your textfield is, By using below code in you textfield's delegate method :

let item:UICollectionViewCell = textField.superview?.superview as! UICollectionViewCell

 let indexPath = collection_view.indexPath(for: item)

 print(" Section :\(indexPath?.section)")
 print(" Row :\(indexPath?.row)")
 print(" Item :\(indexPath?.item)")
let collectionIndexPath = textfield.superview?.superview as! UICollectionViewCell
print(collectionIndexPath.row)
print(collectionIndexPath.section)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM