Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView scrollToRowAtIndexPath only scrolls sometimes
    primarykey
    data
    text
    <p>I have a UITableView with several UITableViewCells and inside these cells are UITextFields. I implemented a UIToolbar for switching between the different textfields. I can go to the next textfield in the next cell or to the previous textfield. This works fine until a cell should become first responder which is currently not visible in the tableview.</p> <p>I figured out that </p> <pre><code>[self.tableview cellForRowAtIndexPath:indexPath] </code></pre> <p>returns <code>nil</code> and that manually scrolling to the cell with the textfield, which should become first responder, removes the problem. Therefore I tried scrolling to the cell before changing it to become first responder.</p> <p>I tried that with </p> <pre><code>[[self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; </code></pre> <p>but unfortunately the tableview does not scroll to this cell but to all other cells. When I add a breakpoint to this line the problem doesn't occur. It also looks like the tableview is scrolling to the cell but then scrolls down again. </p> <p>Here is the code of cellForRowAtIndexPath:</p> <pre><code>UITableViewCell *cell; static NSString *AttributeCellIdentifier = @"AttributeCellIdentifier"; cell = [tableView dequeueReusableCellWithIdentifier:AttributeCellIdentifier]; if (cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"AttributeCell" owner:self options:nil]; cell = attributeCell; self.attributeCell = nil; UITextField * textField = (UITextField *)[cell viewWithTag:1]; [textField setKeyboardType:UIKeyboardTypeDecimalPad]; [textField setInputAccessoryView:[self inputAccessoryView]]; } UITextField * textField; textField = (UITextField *)[cell viewWithTag:1]; textField.placeholder = @"C(Probe)/mol/l"; return cell; </code></pre> <p>Here is the code for switching to the previous textfield:</p> <pre><code>UIView * currentResponder = [self.view findFirstResonder]; UITextField * newFocusTextField; UITableViewCell * cell = (UITableViewCell*)[[currentResponder superview] superview]; NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; newFocusTextField = (UITextField*)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] viewWithTag:1]; [currentResponder resignFirstResponder]; [newFocusTextField becomeFirstResponder]; UITableViewCell * newCell = (UITableViewCell*)[[newFocusTextField superview] superview]; NSIndexPath * newIndexPath = [self.tableView indexPathForCell:newCell]; [self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload