Reputation: 35050
When calling textView becomeFirstResponder
from cellForRowAtIndexPath
returns false, why?
But from other method i.e. from didSelectRowAtIndexPath
it works.
Is it in connection that I am using iOS 8 introduced UITableViewAutomaticDimension
row height approach?!
Upvotes: 1
Views: 908
Reputation: 5806
hijack viewDidAppear for the purposes like so:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let indexPath = NSIndexPath(forRow: whatever, inSection: 0mostlikely)
if let cell = tableView.cellForRowAtIndexPath( indexPath) as? YourTableViewCellClass {
cell.textField.becomeFirstResponder()
}
}
viewDidLayoutSubviews is more expensive cause its called gazillion times whilst viewDidAppear is called only once
Upvotes: -1
Reputation: 11201
Try calling becomeFirstResponder in a custom method of your table view cell like layoutSubviews because at that point the text view will have been added to the view hierarchy. If you call it in cellForRowAtIndexPath method the text view has not been added to the view hierarchy yet and it does not work.
- (void)layoutSubviews{
// Initialization code
if ([self.textView becomeFirstResponder]) {
NSLog(@"yes");
}
}
Upvotes: 0
Reputation: 2011
When you check the documentation it says that the view have to be in the view hierarchy. I think in the cellForRowAtIndexPath method the text view is not yet attached to the hierarchy but when the cell is returned.
You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.
Upvotes: 2