Dan L Dan L - 7 months ago 16
Swift Question

Detect Which Cell a User is in With Custom Table View Cell

I have a custom tableViewCell with two (2) textViews for a user to type in. I want to detect which tableView row the user is in. I tried:

let currentIndexPath = tableView.indexPathForSelectedRow!

let currentCell = tableView.cellForRowAtIndexPath(currentIndexPath) as! Test2TableViewCell

and Xcode gives me the error,
fatal error: unexpectedly found nil while unwrapping an Optional value
which, to me, means that the app can't find the selected row. I'm assuming that when a user types in a textView on top of a tableViewCell, the app doesn't see you tapping on the actual cell. Is there a way to detect which row the user is in?


Consider using the tag property present on all UIView subclasses.

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell"){
         cell.textView.tag = indexPath.row


internal func textViewDidBeginEditing(textView: UITextView){
    let row = textView.tag