我有一个使用自定义 UITableViewCell 的 UITableView。我的 UITableVewCell 内部有两个文本字段,我在主 ViewController 中添加了一个标签,该标签包含包含我的自定义 UITableViewCell 的 UITableView。
所以这是我的 tableView:cellForRowAIndexPath:
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomFCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *widthString = [currentFDictionary objectForKey:@"MM"];
if ((NSNull *) widthString != [NSNull null]) {
cell.widthTexField.text = widthString;
cell.widthTexField.tag = indexPath.row;
} else {
cell.widthTexField.text = @" ";
}
NSString *heightString = [currentFDictionary objectForKey:@"NM"];
if ((NSNull *) heightString != [NSNull null]) {
cell.heightTextField.text = heightString;
cell.heightTextField.tag = indexPath.row;
} else {
cell.heightTextField.text = @" ";
}
return cell;
我想知道如何使用此 .tag
将 UITableViewCell 滚动到现在将显示在 View 中的 UIKeyboard 上方。
任何帮助将不胜感激。
最佳答案
我能想到的一个简单方法是使用 -scrollToRowAtIndexPath:atScrollPosition:animated:
在 UITextField
对象上设置委托(delegate)。
cell.widthTexField.delegate = self;
//...
cell.heightTextField.delegate = self;
现在,以这种方式使用 -textFieldShouldBeginEditing:
委托(delegate)方法:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSIndexPath *currentSelectedIndexPath = [self.tableView indexPathForSelectedRow];
[self.tableView scrollToRowAtIndexPath:currentSelectedIndexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
//NOTE: this way we don't really need to work with the textField tag per se
//(unless you are using it elsewhere as well)
//Instead, we work with the entire cell and scroll it to the desired position
这可能并不完美,因为我还没有在这里测试过,但这是一般的想法
关于ios - 如何在键盘上方滚动自定义 UITextField 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20452298/