ios - 如何在键盘上方滚动自定义 UITextField 单元格

标签 ios uitableview uikit uitextfield

我有一个使用自定义 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/

相关文章:

iOS swift Wordpress api 获取不到json

ios - TableView 不会加载任何数据

iphone - Core Graphics 未以正确的宽度绘制线条

iphone - 如何使用 iPhone SDK 将表格输出到 PDF 文件?

ios - 从 Web View 中的链接打开 Mobile Safari

ios - 有没有办法禁用导航栏中嵌入的按钮的用户交互(navigationItem.leftBarButtonItem)?

ios - 通用 UITableView

iphone - UIView 不处理触摸

ios - 遍历已解析的数组

iphone - ios UITableView删除/重新排列编辑模式但不显示删除图标