ios - 将文本移动到 UITableViewCell 中的下一个文本字段

标签 ios objective-c uitableview uitextfield

我有一个 UITableView 和多个 UITableViewCell

每个单元格都有一个 UITextFiled

现在我将文本添加到第一行文本字段。现在,当文本范围达到屏幕宽度时,我想将文本移动到第二行文本文件。

编辑:

文本字段委托(delegate)方法

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    CGFloat textWidth = [textField.text sizeWithAttributes:@{NSFontAttributeName : textField.font}].width;
    CGFloat frameWidth = textField.frame.size.width - 11;

    if(textWidth > frameWidth) {

       //text reaches to screen width.
       [textField resignFirstResponder];

       MemoTableViewCell *nextCell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(textField.tag+1) inSection:0]];
       [nextCell.noteTextField becomeFirstResponder];

      return NO;
  }

return YES;

}

表格 View 方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *simpleTableIdentifier = @"SimpleTableItem";

    MemoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
          cell = [[MemoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.noteTextField.delegate = self;
    cell.noteTextField.tag = indexPath.row;

    return cell;
}

现在我想在上述方法返回 NO 时移动文本。

抱歉我的英语不好。我希望您能理解我的问题,如果不明白请问我。

请帮助我。

编辑

上面的代码工作正常,但有时会在下面的行崩溃

CGFloat textWidth = [textField.text sizeWithAttributes:@{NSFontAttributeName : textField.font}].width;

Warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.

最佳答案

尝试下面的代码,也许会有帮助:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


CGFloat textWidth = [textfield.text sizeWithAttributes:@{NSFontAttributeName : textfield.font}].width;
CGFloat frameWidth = textfield.frame.size.width;

    if(textWidth > frameWidth) {

       //text reaches to screen width.
       [textfield resignFirstResponder];

        /*
        //Get next TextField and Make it first responder
        [cell.YourNextTextField becomeFirstResponder]; 
        */

        return NO;
    }

    return YES;
}

关于ios - 将文本移动到 UITableViewCell 中的下一个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40151972/

相关文章:

ios - 使用 macOS Sierra GM (16A322) 和 xCode 8.0 提交 iOS App 以供审核时出错

ios7 没有 displayModeButtonItem 或 targetDisplayModeForActionInSplitViewController

objective-c - 使用尚未呈现的实时数据更新 View ,ios

objective-c - Objective-c中release和dealloc的区别

swift - 如何在动态 uitableview 中隐藏部分及其行

ios - 选定行的自定义复选标记

ios - 如何设置iOS WkWebview缩放比例

ios - 使用 Swift 连续动画化 UIViews 数组

iphone - CoreData关系使用

ios - Swift tableview 找不到接受所提供参数的 'subscript' 重载