[英]UItextField becomeFirstResponder returning No
我有一个自定义UITableViewCells的UITableView,每个UITableViewCells中都有两个UITextField。 当我在打开视图中选择一个UITextField时,即不滚动,请在打开视图中选择一个UITextField。 它变成了FirstResponder,我可以输入文本,当我按下Enter键时,打开视图中的所有UITextField都可以成为FirstResponder,但是当我到达该打开视图的UITableViewCells的末尾并尝试加载当前不在视图中的下一个UITableViewCell并设置其UITextField成为FirstResponder无效。
我在textFieldShouldReturn内部设置了if语句,它确认textField返回NO成为firstResponder。
我想知道如何解决这个问题; 我一直在寻找解决方案,但是还没有找到适合我的情况。 下面是我在这里使用的UITextFieldDelegates,我应该在这里执行类似调用或加载下一个UITableViewCell的操作,但是我不知道如何做。
#pragma mark - Textfield Delegates
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
if ([textField isEqual:cell.widthTexField]) {
nWidth = [[sortedItemsArray objectAtIndex:textField.tag] valueForKey:@"w_MM"];
} else if ([textField isEqual:cell.heightTextField]) {
nHeight = [[sortedItemsArray objectAtIndex:textField.tag] valueForKey:@"h_MM"];
}
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textfield {
int height = self.finishingTableView.frame.size.height;
NSLog(@"%i", height);
self.finishingTableView.frame= CGRectMake(self.finishingTableView.frame.origin.x, self.finishingTableView.frame.origin.y, self.finishingTableView.frame.size.width, 307);
// select correct row
if (textfield.tag > 999999) {
int adjustTag = textfield.tag-1000000; // remove a million so that you have the text fields correct position in the table. (this is only for height textfields)
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:adjustTag inSection:0];
[finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
[self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
return YES;
} else {
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:textfield.tag inSection:0];
[finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
[self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
return YES;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%i", textField.tag+1);
[[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
BOOL success = [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
if (success) {
NSLog(@"HypnosisView became the first responder");
} else {
NSLog(@"Could not become first responder");
}
// this means there has been a change in the UItextfield
NSLog(@"%@", selectedItemDictionary);
if (textField.tag < 999999) {
tempFinishingObjectDictionary = [selectedItemDictionary mutableCopy];
if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"w_MM"]]) {
tempUpdatedRow = @"T";
// remove kevalues
[tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
[tempFinishingObjectDictionary removeObjectForKey:@"new_w_MM"];
// update keyvalues
[tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
[tempFinishingObjectDictionary setValue:textField.text forKey:@"new_w_MM"];
}
} else {
if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"h_MM"]]) {
tempUpdatedRow = @"T";
// remove kevalues
[tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
[tempFinishingObjectDictionary removeObjectForKey:@"new_h_MM"];
// update keyvalues
[tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
[tempFinishingObjectDictionary setValue:textField.text forKey:@"new_h_MM"];
}
}
NSLog(@"%@", tempFinishingObjectDictionary);
[coreDataController editSelectedFinishing:htmlProjID UpdatedNSD:tempFinishingObjectDictionary SelectedRow:selectedItemIndexPathRow];
[SVProgressHUD dismiss];
NSLog(@"%@", selectedItemDictionary);
return YES;
}
在textFieldShouldReturn中,当您转到下一个单元格时,应检查是否有两个单元格不在屏幕上(因此不存在)。 如果是屏幕,则应将表格滚动一个单元格
[tableview setContentOffset:CGPointMake(tableview.contentOffset.x,tableview.contentOffset.y + cellHeight) animated:YES];
您将不得不根据自己的喜好调整此滚动,因为我不知道您的应用程序如何运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.