Iphone 获取UITableViewCell上的UITextField的引用?
实际上,我使用“下一步”和“上一步”按钮将一个单元格移动到另一个单元格,每个单元格都有一个文本字段,因此当我单击“下一步”按钮时,它会将我移动到下一个单元格,通过获取此单元格引用,我可以使文本字段成为第一响应者,但当我单击“上一步”按钮时,它不会返回任何引用。 下面给出了我用于下一个和上一个的代码Iphone 获取UITableViewCell上的UITextField的引用?,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,实际上,我使用“下一步”和“上一步”按钮将一个单元格移动到另一个单元格,每个单元格都有一个文本字段,因此当我单击“下一步”按钮时,它会将我移动到下一个单元格,通过获取此单元格引用,我可以使文本字段成为第一响应者,但当我单击“上一步”按钮时,它不会返回任何引用。 下面给出了我用于下一个和上一个的代码 - (IBAction)nextPrevious:(id)sender { NSIndexPath *indexPath ; BOOL check = FALSE; if([
- (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];
}
-(iAction)下一个上一个:(id)发送方
{
nsindepath*indepath;
布尔检查=假;
如果([(UISegmentedControl*)发送方选择了分段索引]==1){
如果(sectionCount>=0&§ionCount0&§ionCount,问题在于滚动。当滚动到下一行的顶部时,上一行将被删除并重新用于最后一个可见行,这意味着方法cellForRowAtIndexPath:
可能会返回null,因为该单元格当前不可用
快速和肮脏的解决方案包括滚动到中间或稍微移位,使单元格仍然可见。不那么快或肮脏的解决方案包括创建一个程序,滚动表格以确保单元格可见,然后当滚动停止时,将文本字段设置为第一响应者
(Edit)进一步解释最后一种方法。假设您添加了一个新变量nsindepath*indexPathEditing
。委托方法tableView:cellForRowAtIndexPath:
将具有:
if (indexPathEditing && indexPathEditing.row == indexPath.row && indexPathEditing.section == && indexPath.section)
{
// Retrieve the textfield with its tag.
[(UITextField*)[cell viewWithTag:<#Whatever#>] becomeFirstResponder];
indexPathEditing = nil;
}
该行将出现,调用了tableView:cellForRowAtIndexPath:
,它将被自动设置为第一响应者
另外,请注意,与使用isKindOfClass
执行for不同,设置标记号更容易,然后使用viewWithTag:
检索对象,我将此合并到示例中
indexPathEditing = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
[registrationTbl scrollToRowAtIndexPath:indexPathEditing
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
[registrationTbl reloadData];