1

the goal is to create new table row every time any other row is tapped. all rows contain UITextField and that is the problem.

main controller:

var subtasksArray = ["one", "two"]
var addRow = 0

class AddEditTaskController: UIViewController, UITableViewDataSource,
UITableViewDelegate, CustomCellDelegate {

    func reloadTable() {
        subtaskTable.reloadData()
    }

    @IBOutlet weak var subtaskTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var tblView =  UIView(frame: CGRectZero)
        subtaskTable.tableFooterView = tblView
        subtaskTable.tableFooterView?.hidden = true
        subtaskTable.backgroundColor = UIColor.clearColor()
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return subtasksArray.count + addRow
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell: SubtaskCell = subtaskTable.dequeueReusableCellWithIdentifier("subtaskCell") as SubtaskCell

        cell.delegate = self 
        return cell 
    }

custom cell:

protocol CustomCellDelegate {
    func reloadTable()
}

class SubtaskCell: UITableViewCell, UITextFieldDelegate {

    var delegate: CustomCellDelegate?

    @IBOutlet weak var subtaskTextField: UITextField!

    var subtasksArray = [String]()

    override func awakeFromNib() {
        super.awakeFromNib()

        subtaskTextField.delegate = self
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        addRow += 1
        delegate?.reloadTable() 
    }

    func textFieldDidEndEditing(textField: UITextField) {

        subtasksArray.append(subtaskTextField.text)
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

the problem is that if tap any row, a new one is created, but text field is not active.

if i don't use CustomCellDelegate, textfields in cells work fine, but i couldn't find any other way to reload table except cell delegate.

xnn
  • 11
  • 2
  • After you call `tableView.reloadData()`, the `UITextField` won't be active because the whole table was reloaded. Somewhere when you reload it, you need to use the `becomeFirstResponder()` function of `UITextField` to reactivate the field after the data is reloaded. – ad121 Apr 04 '15 at 23:37
  • yes, i thought so before posting the question. i tried to put becomeFirstResponder() after delegate?.reloadTable() , but it didn't help. i also tried to do what was suggested here http://stackoverflow.com/questions/11372589/uitableview-reloaddata-causes-uitextfield-to-resignfirstresponder but it didn't work either. – xnn Apr 05 '15 at 07:55
  • You could try passing back the indexPath of the cell to your TVC and save it as a variable and in the cellForRowAtIndexPath function check if the indexPath is equal to that saved variable, if it is do textField.becomeFirstResponder(). I haven't tested this but I think it would work. You might have to adjust your implementation a little to pass back the indexPath though. Hope this helps. – ad121 Apr 05 '15 at 20:32
  • unfortunately i don't know how to implement that.. but goal i'm trying to achieve can be a bit simpler. since there are 2 rows in the begging, i need to reload table after user has put something in the 1st row. so if i just put `addRow += 1 delegate?.reloadTable()` to `textFieldDidEndEditing` it almost works. when user done typing in 1st text field and taps on second text field, 3rd text field is created, but in order to type in 2nd text field user has to tap that text field again. putting `subtaskTextField.becomeFirstResponder()` in any place doesn't help anything.. not sure why ( – xnn Apr 06 '15 at 11:28

0 Answers0