Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

make custom cell's text field active after reloadData (swift)

main view controller:

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

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!

    override func awakeFromNib() {
        super.awakeFromNib()

        subtaskTextField.delegate = self
    }

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

        subtaskTextField.becomeFirstResponder()
    }

    func textFieldDidEndEditing(textField: UITextField) {

    }

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

i'm trying to make a new row each time a row i tapped. it works, but text field inside row stays inactive even with subtaskTextField.becomeFirstResponder() that is after delegate?.reloadTable()

is there any way around that?