我像这样创建表格单元格(它是第一部分的第一个单元格,也是唯一一个带有 textField 的单元格):
if (indexPath.section == 0 && indexPath.row == 0) {
GroupedTableViewCellWithTextField *cell = (GroupedTableViewCellWithTextField *) [tableView dequeueReusableCellWithIdentifier: nameCellIdentifier];
if (!cell && !_nameCell) {
cell = [[GroupedTableViewCellWithTextField alloc] initWithStyle: UITableViewCellStyleValue1
reuseIdentifier: nameCellIdentifier];
cell.textField.delegate = self;
if (!_adding)
cell.textField.text = @"Some Text";
_nameCell = cell;
}
if (!cell) {
cell = _nameCell;
}
return cell;
}
textField 是由一个简单的
- (UITextField *)textField {
if (!_textField) {
CGRect rect = self.bounds;
CGFloat cellWidth = rect.size.width;
CGFloat cellHeight = rect.size.height;
CGRect cellRect = CGRectMake(rect.origin.x + 15, rect.origin.y, cellWidth - 48, cellHeight);
_textField = [[SSTextField alloc] initWithFrame: cellRect];
_textField.textColor = self.textLabel.textColor;
_textField.placeholderTextColor = [UIColor grayColor];
_textField.placeholder = @"Name";
_textField.backgroundColor = [UIColor clearColor];
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.textAlignment = UITextAlignmentCenter;
_textField.returnKeyType = UIReturnKeyDone;
_textField.alpha = 1.0f;
[_textField becomeFirstResponder];
[self.contentView addSubview:_textField];
}
return _textField;
}
然后我有:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([_nameCell.textField isFirstResponder]) {
[_nameCell.textField resignFirstResponder];
return YES;
}
问题如下:如果我向下滚动,直到带有 textField 的单元格超出屏幕并且我 resignFirstResponder,我得到
no index path for table cell being reused
但只有一次。如果我用 textField 触摸单元格内部,它的 textField 再次获得 firstResponder,然后再次向下滚动并 resignFirstResponder,我不会再次收到该错误......它只显示第一次。
如果我
[self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0]
atScrollPosition: UITableViewRowAnimationTop
animated: YES];
滚动太慢了,我仍然得到错误。如果我不做动画,我就不会再收到错误了,因为单元格的显示速度比调用 resignFirstResponder 的速度快;但它看起来和感觉都很丑。
我也尝试过辞职FirstResponder
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
和
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
所以它发生在单元格离开视图之前,但动画很不稳定:滚动开始,然后在键盘开始向下移动时停止一秒钟,然后再次开始。
只是为了让自己清楚,resignFirstResponder 有效。
我应该注意,表格足够大,需要在显示键盘时滚动,但足够小以在不显示键盘时适合屏幕。
此外,如果向下滚动表格直到单元格不再出现在屏幕上,如果我弹出视图,也会出现错误。
我想保持表格可滚动,我知道我可以关闭它。
关于如何阻止错误出现的任何想法,但又不会让应用程序感觉不稳定或没有动画效果?其他一切正常:数据保存、数据显示、响应程序、我没有崩溃等。
我知道这不是一个严重错误,我可以让它成为,但我仍然希望没有错误:)
谢谢你。