【发布时间】:2014-01-06 17:48:55
【问题描述】:
我有一个自定义 UITableViewCells 的 UITableView,每个都有两个 UITextFields。当我在打开视图中选择 UITextField 时,即不滚动选择打开视图中的 UITextField。它成为FirstResponder,我可以输入文本,因为我在打开视图中输入所有 UITextFields 可以成为FirstResponder 但是当我到达该打开视图的 UITableViewCells 的末尾并尝试加载当前不在视图中的下一个 UITableViewCell 并设置其UITextField to becomeFirstResponder 它不起作用。
我在 textFieldShouldReturn 中设置了一个 if 语句,它确认 textField 正在返回 NO 给 becomeFirstResponder。
我想知道如何解决这个问题;我一直在寻找解决方案,但还没有找到适合我情况的任何东西。下面是我正在使用的 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;
}
【问题讨论】:
标签: ios objective-c uitableview uitextfield