【发布时间】: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