本文介绍了resignFirstResponder to UITextViews on UITableViewCells 不再出现在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,里面有一大堆自定义的 UITableViewCells.这些 UITableViewCells 中的每一个都有一个 UITextView.

I have a UITableView with a whole bunch of custom UITableViewCells in it. Each of these UITableViewCells has a UITextView in them.

我遇到的问题是,如果用户触摸 UITextView,它会成为 firstResponder 并且键盘会弹出.如果用户然后向下滚动到 firstResponder 所在的 UITableViewCell 不再出现在屏幕上的点,我将无法通过 UITextView 关闭它.

The problem I'm having, is that if the user touches a UITextView, that becomes the firstResponder and the keyboard pops up. If the user then scrolls down to a point where the UITableViewCell that the firstResponder is on is no longer on the screen, I can't get at the UITextView to dismiss it.

我尝试保留(并保留)对 UITextView 的引用,但是一旦它离开屏幕并尝试访问它,我就会得到一个 EXC_BAD_ACCESS.无论我保留多少次都会发生这种情况,所以我怀疑它可能直接在其上调用了 dealloc(我不确定单元格出列系统实际上是如何工作的).

I've tried holding (and retaining) a reference to the UITextView, but as soon as it leaves the screen and I try to access it I get an EXC_BAD_ACCESS. This happens no matter how many times I retain it, so I suspect it may be having dealloc called directly on it (I'm not sure how the cell dequeueing system actually works).

另一个奇怪的事情是,如果我将 UITextField 滚动到屏幕外,仍然可以使用键盘对其进行写入,并且当我滚动回 时可以看到键入的文本UITextField.

The other weird thing is that if I scroll the UITextField off screen, the keyboard can still be used to write to it, and the text typed is visible when I scroll back to the UITextField.

任何帮助将不胜感激.

推荐答案

Fluchtpunkt 是绝对正确的,我没有正确地重复使用我的单元格.我的问题是我正在使用以下方法从笔尖加载我的自定义 UITableViewCells:

Fluchtpunkt was absolutely correct, I wasn't re-using my cells correctly. My problem was that I was loading my custom UITableViewCells from a nib using the method:

static NSString *CellIdentifier = @"CustomCell";
ExpandingTableViewCell *cell = (ExpandingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExpandingTableViewCell" owner:self options:nil];
    cell = (ExpandingTableViewCell *)[nib objectAtIndex:0];
}

我没有意识到我必须将自定义单元格的 .xib 文件中的 Identifier 字段设置为 CustomCell.一旦我的单元格被正确地重新使用,UITextField 在它的时间消失之前被释放的问题消失了.

I didn't realize that I had to set the Identifier field in the .xib file for the custom cell to CustomCell. As soon as my cells were being re-used properly, the problem with the UITextField being dealloc'd before its time disappeared.

这篇关于resignFirstResponder to UITextViews on UITableViewCells 不再出现在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:23