0

I would like to keep subviews (UITextFields in this case) still active (editable in usual way i.e. tap on the textField and a keyboard appears) while the parent view which is a UITableViewCell's userInteractionEnabled is set to false.

Explaining further - 1. There is a UITableViewCell initialized from a xib.

  1. Inside this cell, I have three UITextFields and a UIButton.
  2. On some condition, I need to disable the cell interaction (to avoid the didSelectRowAtIndexPath) - this part, I do not have any problem.
  3. After step 3, all the subViews inside the cell are getting disabled as well.
  4. But I need to enter some text inside the textFields and tap the button with some action to it.

What I did so far: 1. Used userInteractionEnabled and set it to false for the cell -

myPersonalInfoExpandedCell.userInteractionEnabled = false

2. Two textFields firstNameText and lastNameText, I enabled userInteraction and set it firstNameText to becomeFirstResponder

 (myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).firstNameText.userInteractionEnabled = true
 (myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).lastNameText.userInteractionEnabled = true
 (myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).firstNameText.becomeFirstResponder()

Problem is, for the textFields, the userInteractionEnabled is not working which I suspect because of userInteractionEnabled is false for the TableViewCell. The becomeFirstResponder() is working for firstNameText but I need interaction to all the UI Elements inside the cell.

How can we do that? Please could someone help?

Lohith Korupolu
  • 1,066
  • 1
  • 18
  • 52

1 Answers1

2

To disable the cellSelection implement the willSelectRowAtIndexPath delegate method and return nil for the cells that you do not want to select. You say that you want to disable selection under some conditions, so I assume that you have the indexPaths for the rows for which you want to disable selection. Just return nil for these indexPaths. See the following code.

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == self.MyIndex) //Check for the index path for the cell you want to disable selection for
   return nil;
else
   return indexPath;
}

Also do this to prevent the cell from highlighting.

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Doing it this way you do not have to setUserInteractionEnabled:NO on the cell. This way when you tap on the cell the didSelectRowAtIndexPath delegate method won't get called and your textFields would still be enabled

Edit

Following is the swift version of the above code

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if (indexPath == self.MyIndex) //Check for the index path for the cell you want to disable selection for
    return nil;
else
    return indexPath;
}


cell.selectionStyle = .None
Shayan Jalil
  • 588
  • 5
  • 17
  • Thanks for the answer Shayan! return false for the cells I do not want to select? I didn't get that. Could you quote an example please? – Lohith Korupolu Sep 26 '16 at 07:51
  • @LohithKorupolu I have edited my answer. Take a look at it. If you still have questions feel free to ask – Shayan Jalil Sep 26 '16 at 08:04
  • It's working, I am able to edit my textFields but have an issue with my UIButton inside the same view. When I tap the button, I get an exception libc++abi.dylib: terminating with uncaught exception of type NSException – Lohith Korupolu Sep 26 '16 at 09:33
  • Can your provide more log details? – Shayan Jalil Sep 26 '16 at 10:05
  • I only get that single line in my log. I also checked if it's an issue in storyboard or in .swift file (I mean, referencing the Button in my ViewController swift file) but everything looks good – Lohith Korupolu Sep 26 '16 at 10:14
  • 1
    http://stackoverflow.com/questions/26442414/libcabi-dylib-terminating-with-uncaught-exception-of-type-nsexception-lldb Check out this link. It most probably is a problem with your IBOutlet. Remove all the connections and add them again – Shayan Jalil Sep 26 '16 at 10:24
  • 1
    Yeah, I already looked at it. But may be I should remove the connection and re-add them. But for the actual answer, I am accepting it :-) – Lohith Korupolu Sep 26 '16 at 10:26