自定义tableViewCell中的textField resignFirstResponder后出现tableView frame.origin问题 [英] tableView frame.origin problem after textField resignFirstResponder in custom tableViewCell

查看:47
本文介绍了自定义tableViewCell中的textField resignFirstResponder后出现tableView frame.origin问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有自定义单元格的tableView.当我想使用navigationController的pushViewController函数显示另一个视图时,我遍历文本字段并对其调用resignFirstResponder.但是resignFirstResponder仅在显示文本字段时才起作用,因此我首先滚动到页面顶部.这是代码:

I'm using an tableView with custom cells. When I want to display another view using the pushViewController function of the navigationController I loop through the textfields and call resignFirstResponder on them. But resignFirstResponder does only work when the textfields are being displayed so I scroll first to the top of the page. This is the code:

NSIndexPath *topIndexPath;
topIndexPath = [[NSIndexPath indexPathWithIndex:0] indexPathByAddingIndex:0];
[self.tableView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

[[self textFieldForRow:0] resignFirstResponder];
[[self textFieldForRow:1] resignFirstResponder];
[[self textFieldForRow:2] resignFirstResponder];
[[self textFieldForRow:3] resignFirstResponder];

这可行,但是在此之后,我的tableView的来源出现了一些奇怪的问题.我试图将其超级视图原点设置为0,但这无济于事.

This works, but after this my tableView has some weird problem with its origin. I tried to set it's superviews origin to 0, but that doesn't help.

以下是问题的屏幕截图:链接

Here is a screenshot of the problem: link

如您所见,我的表视图太大,到达底部时滚动条卡在视图的中间.

As you can see, my tableview is too large and the scrollbar is stuck in the middle of the view when reaching the bottom.

对不起,我的英语,希望您能理解我,

Sorry for my english, I hope that you can understand me,

提前谢谢!

汉斯

推荐答案

实际上非常简单.只需将您的resignFirstResponder放在-viewWillDisappear

It was actually quite simple. Just put your resignFirstResponder in -viewWillDisappear

这样做更好,而且不那么笨拙,我将其添加到了我的课上,并且有效:

edit: this is better and less hacky, I added this to my class, and it worked:

edit 2:似乎您的应用在使用前面的代码时将被拒绝.这是更新的公共api版本:

edit 2: seems that your app will be rejected when using the previous code. Here is a updated public api version:

- (void)viewWillDisappear:(BOOL)animated
{
    [self.view findAndResignFirstResponder];
}

并且:

@implementation UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResponder
{
    if (self.isFirstResponder) {
        [self resignFirstResponder];
        return YES;     
    }
    for (UIView *subView in self.subviews) {
        if ([subView findAndResignFirstResponder])
            return YES;
    }
    return NO;
}
@end

(来源:获取当前的第一响应者,而无需使用私有API )

这篇关于自定义tableViewCell中的textField resignFirstResponder后出现tableView frame.origin问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆