【问题标题】:Move text to next textfiled in UITableViewCell将文本移动到 UITableViewCell 中的下一个文本字段
【发布时间】:2016-10-20 10:34:36
【问题描述】:

我有一个UITableView 和多个UITableViewCell

每个单元格有一个UITextFiled

现在我正在向文本文件的第一行添加文本。现在我想在文本范围达到屏幕宽度时将文本移动到文本文件的第二行。

编辑:

Textfiled 委托方法

 - (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;

}

表格视图方法:

- (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;

警告: 无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。

【问题讨论】:

  • “我要移动文本”是什么意思?您想让下一个textField 成为第一响应者,还是要将文本从当前textField 移动到下一个?
  • @Adeel 我希望下一个单元格文本文件在其文本达到屏幕宽度时成为comFirstResponder。
  • 检查您的文本字段委托连接。你在哪里设置的?在 Fileowner 或 View 中?我怀疑您可能已将其连接到 View
  • @Wolverine 是的,我已将其连接到查看。
  • 委托必须连接到控制器的文件所有者。如果您为单元格设置了 .xib,则尝试在 cellforRow 中以编程方式设置它们的委托,如果没有,则将它们设置为控制器的文件所有者,您可以在情节提要中看到。

标签: ios objective-c uitableview uitextfield


【解决方案1】:

试试下面的代码,可能会有所帮助:

- (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;
}

【讨论】:

  • 谢谢,我会尽力让你知道的。
  • 当然,如果您有任何问题,请告诉我@EktaPadaliya
  • 它工作正常,但收到此警告 无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。
  • 我认为与上述代码无关,请尝试搜索或发布新问题@EktaPadaliya
  • 如果它有助于解决您的问题,请将其标记为答案,因此它将帮助其他@EktaPadaliya
【解决方案2】:

这是您可以应用来实现您的要求的高级逻辑。

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

   CGSize size = [textField.text sizeWithAttributes:
@{NSFontAttributeName: [UIFont systemFontOfSize:textField.font]}];

   if (size.width > textField.frame.size.width) {

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

       /*
          //Get next TextField and Make it first responder
          [cell.myTextField becomeFirstResponder]; 
       */
       /*
        Add code logic here
        1. Get the next cell instance - using cellForRowAtIndexPath
        2. Get its textfield instance - cell.myTextField
        3. Make that one as first responder - [cell.myTextField becomeFirstResponder]; 
        */
       return NO;
   }

   return YES;
}

这会将下一个单元格的文本字段设置为主要输入,并将您的单元格跳转到该限制。

您可能需要相应地设置视图位置。

希望这会有所帮助。

【讨论】:

  • 感谢您的宝贵时间和宝贵的回答。我会尽力让你知道。
  • 那个 textfiled 委托方法只能工作到 25 个字符。我想将文本添加为​​屏幕宽度。你收到我的问题了吗?
  • 它工作正常,但收到此警告 无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。
  • @EktaPadaliya 更新了答案 - 在这里您需要测量字体文本宽度与文本字段的宽度相匹配,并相应地走得更远。
  • @EktaPadaliya:现在检查。
【解决方案3】:

我假设您的 tableView 只有一个部分。用这个替换shouldChangeCharactersInRange:中的代码。

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

BOOL shouldChange = YES;

NSString *text = [textField.text stringByAppendingString:string];

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

CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

if (textWidth > screenWidth) {

    shouldChange = NO;

    NSInteger cellIndex = textField.tag;

    NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];

    if (cellIndex < numberOfRows - 1) {

        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:cellIndex+1 inSection:0];

        [self.tableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

        TableViewCell *cell = [self.tableView cellForRowAtIndexPath:nextIndexPath];

        [cell.textField becomeFirstResponder];
    }
}

return shouldChange;
}

别忘了在cellForRowAtIndexPath: 中设置textField 的标签,即cell.textField.tag = indexPath.row

【讨论】:

  • 同样的问题 无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。 我收到此警告。
  • 您在哪一行收到此警告?
  • CGFloat textWidth = [文本 sizeWithAttributes:@{NSFontAttributeName: textField.font}].width;
  • 代码是否按预期工作,因为我没有收到任何警告,而且代码也工作正常。您是否尝试过清理您的项目或重新启动 Xcode?​​span>
猜你喜欢
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多