开发者

UITableView scrollToRowAtIndexPath only scrolls sometimes

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.

I figured out that

[self.tableview cellForRowAtIndexPath:indexPath]

returns nil 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.

I tried that with

[[self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

but unfortunately the tableview does not scroll to this cell but to all other cells. When I add a breakpoint to this lin开发者_JAVA百科e the problem doesn't occur. It also looks like the tableview is scrolling to the cell but then scrolls down again.

Here is the code of cellForRowAtIndexPath:

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;

Here is the code for switching to the previous textfield:

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];

Hope this helps.


The code on your post and the problem definition is not hundred percent clear, but there was a bug/glitch in scroll methods' working for iOS 3.0 (i guess specifically 3.0, as far as i remember). I couldnot figure out the reason for it to happen, but it was crashing all the time, although it was OK for iOS 4.x.

The fix was adding a

[table reloadData] 

before the scroll line. I know it doesnt make sense, and I know it is not good practice especially if the cell rendering is expensive, but it did solve the problem for that case. I dont know if your problem stems from the same issue, but you may just give it a try...


I tried several thinks and everything works now fine. I removed:

[currentResponder resignFirstResponder];

And after removing that line the tableview scrolled fine and I can directly set the pointer to the new cell with the newFocusTextField and make it firstResponder with:

[newFocusTextField becomeFirstResponder];

I have a nextTextField method which works fine with

[currentResponder resignFirstResponder];

so I don't really understand why the problem occurs but I think scrolling to the cell and then just setting the new firstResponder is better then resigning the currentResponder first.

Thank you for helping anyway ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜