如何触及我的UITableViewCell以外的东西? [英] How can I tell when something outside my UITableViewCell has been touched?

查看:104
本文介绍了如何触及我的UITableViewCell以外的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于此问题,我有一个具有UITextField的UITableViewCell的自定义子类。当用户触摸不同的表视图单元格或表外的东西时,除了键盘之外,它的工作正常不会消失。我试图找出最好的地方找出在单元格之外的东西被触摸,然后我可以在文本字段上调用resignFirstResponder。

Similar to this question I have a custom subclass of UITableViewCell that has a UITextField. Its working fine except the keyboard for doesn't go away when the user touches a different table view cell or something outside the table. I'm trying to figure out the best place to find out when something outside the cell is touched, then I could call resignFirstResponder on the text field.

如果UITableViewCell可以在其视图之外接收触摸的触摸事件,那么它可能只是自动重命名FirstResponder,但是我没有看到任何方式在单元格中获取这些事件。

If the UITableViewCell could receive touch events for touches outside of its view then it could just resignFirstResponder itself but I don't see any way to get those events in the cell.

em>编辑:我在UITableViewCell子类中尝试了这个(下面),但是它不起作用,我认为因为touchesBegan:withEvent:如果事件由控件处理,则不会被调用。我想我需要抓住这些事件,然后才能以某种方式发送响应者链。

I tried this (below) in my UITableViewCell subclass but it doesn't work, I think because touchesBegan:withEvent: doesn't get called if the event was handled by a control. I think I need to catch the events before they get send down the responder chain somehow.

我正在考虑的解决方案是添加一个touchesBegan:withEvent :方法到视图控制器。在那里,我可以发送一个resignFirstResponder到所有可以看到的表格单元格,除了触摸的内容之外(让它获得触摸事件并处理它)。

The solution I'm considering is to add a touchesBegan:withEvent: method to the view controller. There I could send a resignFirstResponder to all tableview cells that are visible except the one that the touch was in (let it get the touch event and handle it itself).

也许像这样的伪代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint touchPoint = // TBD - may need translate to cell's coordinates

    for (UITableViewCell* aCell in [theTableView visibleCells]) {
        if (![aCell pointInside:touchPoint withEvent:event]) {
             [aCell resignFirstResponder];
        }
    }
}

我不知道这是最好的办法。看来,tableviewcell本身似乎没有办法接收事件通知。

I'm not sure if this is the best way to go about this. There doesn't seem to be any way for the tableviewcell itself to receive event notifications for events outside its view.

EDIT2:我以为我有一个答案(我甚至把它作为一个答案)使用hitTest:withEvent:但没有解决。它并不总是被调用。 : - (

I thought I had an answer (I even posted it as an answer) using hitTest:withEvent: but that didn't work out. It doesn't always get called. :-(

推荐答案

好的,我终于想出了一个完全可以工作的解决方案,我将UITableView子类化,并覆盖了hitTest:withEvent:方法,它被调用为所有触摸在表格视图中的任何地方,导航栏或键盘中唯一可能的触摸都是,而tableview的hitTest不需要知道这些。

OK, I finally figured a solution that fully works. I subclassed UITableView and overrode the hitTest:withEvent: method. It gets invoked for all touches anywhere in the table view, the only other possible touches are in the navbar or keyboard and the tableview's hitTest doesn't need to know about those.

这跟踪活动单元格在表视图中,并且每当您点击不同的单元格(或非单元格)时,它将向停用的单元格发送一个resignFirstResponder,这样可以隐藏其键盘(或其日期戳)。

This keeps track of the active cell in the table view, and whenever you tap a different cell (or non-cell) it sends a resignFirstResponder to the cell going inactive, which gives it a chance to hide its keyboard (or its datepicker).

-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    // check to see if the hit is in this table view
    if ([self pointInside:point withEvent:event]) {
        UITableViewCell* newCell = nil;

        // hit is in this table view, find out 
        // which cell it is in (if any)
        for (UITableViewCell* aCell in self.visibleCells) {
            if ([aCell pointInside:[self convertPoint:point toView:aCell] withEvent:nil]) {
                newCell = aCell;
                break;
            }
        }

        // if it touched a different cell, tell the previous cell to resign
        // this gives it a chance to hide the keyboard or date picker or whatever
        if (newCell != activeCell) {
            [activeCell resignFirstResponder];
            self.activeCell = newCell;   // may be nil
        }
    }

    // return the super's hitTest result
    return [super hitTest:point withEvent:event];   
}    

在具有UITextField的UITableViewCell子类中,我添加以下代码以获取删除键盘(或日期选择器,它像键盘一样向上滑动):

In my UITableViewCell subclasses that have a UITextField, I add the following code to get rid of the keyboard (or date picker, which slides up just like the keyboard):

-(BOOL)resignFirstResponder
{   
    [cTextField resignFirstResponder];  
    return [super resignFirstResponder];
}

Yay!

这篇关于如何触及我的UITableViewCell以外的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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