Getting reference of a UITextField which is on a UITableViewCell?
Actually i am using next and previous button for moving one to another cell and each cell has a textfield so when i am clicking on next button it moves me to the next cell and by getting this cell reference i can make the text field become first responder but when i am clicking on previous button it returns me no reference. The code which i am using for next and previous is given below
- (IBAction)nextPrevious:(id)sender
{
NSIndexPath *indexPath ;
BOOL check = FALSE;
if([(UISegmentedControl *)sender selectedSegmentIndex] == 1){
if(sectionCount>=0 && sectionCount<8){
//for next button
check = TRUE;
sectionCount = sectionCount+1;
indexPath = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
}
}else{
//for previous button
if(sectionCount>0 && sectionCount<=9){
check = TRUE;
sectionCount = sectionCount-1;
indexPath = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
}
}
if(check == TRUE){
//[registrationTbl reloadData];
UITableViewCell *cell = [registrationTbl cellForRowAtIndexPath:indexPath];
for(UIView *view in cell.contentView.subviews){
if([view isKindOfClass:[UITextField class]]){
[(UITextField *)view becomeFirstResponder];
开发者_如何学运维 break;
}
}
[registrationTbl scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
// UITextField *field = (UITextField *) [cell.contentView viewWithTag:indexPath.section];
// [field becomeFirstResponder];
}
Any small suggestion will be much appreciated. Thanks in advance
The problem lies in the scrolling. When you scroll to the top of the next row, the previous row gets removed and reused for the last visible row, meaning that the method cellForRowAtIndexPath:
will probably return null, as the cell is not currently available.
The quick&dirty fix would involve scrolling to Middle or a little displaced so the cell is still visible. The not-so-quick-nor-dirty would involve making a procedure that scrolls the table to make sure the cell is visible, and then when the scrolling stops, set the textfield it as the first responder.
(Edit) To explain a little more this last approach. Let's say that you add a new variable NSIndexPath *indexPathEditing
. The delegate method tableView:cellForRowAtIndexPath:
would have:
if (indexPathEditing && indexPathEditing.row == indexPath.row && indexPathEditing.section == && indexPath.section)
{
// Retrieve the textfield with its tag.
[(UITextField*)[cell viewWithTag:<#Whatever#>] becomeFirstResponder];
indexPathEditing = nil;
}
This means that if indexPathEditing
is set, and the current row that is being loaded is visible, it will automatically set itself as the firstResponder
.
Then, for example (in your nextPrevious:
method), all you need to do is:
indexPathEditing = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
[registrationTbl scrollToRowAtIndexPath:indexPathEditing
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
[registrationTbl reloadData];
The row will appear, the tableView:cellForRowAtIndexPath:
called, and it will get automatically set as the firstResponder
.
Also, notice that instead of doing a for with isKindOfClass
, it's easier to set a tag number, and then retrieve the object with viewWithTag:
, I incorporated this in the example.
精彩评论