我有一个 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/