1

I have a UItableView with custom UItableViewCell. Each cell have UItextField in it. Now i want user to be able to move to next textfield by hitting next button on keyboard. I already know that we can do following in cellForRowAtIndexPath

cell.textField.tag =  indexPath.row

And then for moving to next field

if let nextField = self.view.viewWithTag(textField.tag + 1) as? UITextView {
            nextField.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }

But in my case we i have multiple sections inside UItableview so this

cell.textField.tag = indexPath.row

won't work. Any leads? Thanks in advance.

ishwardgret
  • 790
  • 6
  • 10

2 Answers2

0

You can do it either 1) by taking textfield's tag as combination of section & row

  textfield.tag = indexPath.section*1000 + indexPath.row

and get it by using blow code :

let Row = textfield.tag % 1000
let Section = (textfield.tag - Row)/1000 

or 2) You can get the section and Row value of the TableView Cell in which your textfield is, By using below code in any of textfield's delegate method :

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

 let indexPath = table_view.indexPath(for: item)

 print(" Section :\(indexPath?.section)")
 print(" Row :\(indexPath?.row)")

check the section & row value and then set textfield's becomeFirstResponder with conditions required(as per your number of section and rows)

Krishna Maru
  • 116
  • 13
0

Thanks all for your answer my bad i wasn't able to understand i did it by tweaking my datasource a little

  func getBaseValueCount(currentSection:Int) -> Int {
    var count = 0
    if currentSection == 0 {
        return count
    }
    else{
        for i in 0..<currentSection {
            count = count + (self.formSections[i].fields?.count)!
        }
    }

    return count
}

And then in cellForRowAtIndexPath

 cell.txtAnswer.tag = self.getBaseValueCount(currentSection: indexPath.section) + indexPath.row

Thanks Again.