2

我想要做的是处理触摸事件,这样当我的屏幕被触摸时,我想采取一些行动。例如更改背景颜色我尝试过的事情://我将表视图控制器子类化

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
     tableView.backgroundColor = UIColor.orangeColor()
}

那没有用,我怀疑 tvc 可能不是第一响应者,因此表格视图处理触摸事件。所以我尝试了:

 override func viewDidLoad() {
     super.viewDidLoad()
     tableView.resignFirstResponder()    
}

也试过:

override func becomeFirstResponder() -> Bool {
    return true
}

override func canBecomeFirstResponder() -> Bool {
    return true
}

它们都不起作用。我该如何处理事件?我错过了什么?

编辑

根据本机 swift 代码选择的答案:

override func viewDidLoad() {
        super.viewDidLoad()

        var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tap:")
        tapGestureRecognizer.cancelsTouchesInView = true
        self.tableView.addGestureRecognizer(tapGestureRecognizer)




    }

    func tap(recognizer: UITapGestureRecognizer) {
        if recognizer.state == UIGestureRecognizerState.Ended {
            var tapLocation  = recognizer.locationInView(self.tableView)
            var tapIndexPath : NSIndexPath? = self.tableView.indexPathForRowAtPoint(tapLocation)

            if let index = tapIndexPath  {

                self.tableView(self.tableView, didSelectRowAtIndexPath: index)
            } else {
                self.tableView.backgroundColor = UIColor.orangeColor()
            }
        }

    }
4

2 回答 2

5

如果您想对整个视图的触摸做出反应,而不仅仅是单元格,请在 viewDidLoad 中添加一个轻击手势识别器:

- (void)addTapGestureForListTable {
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnView:)];
    tapGestureRecognizer.cancelsTouchesInView = YES;
    [self.tableView addGestureRecognizer:tapGestureRecognizer];
}

然后实现方法userTappedOnView。如果您想区分是否触摸单元格,请按如下方式实现:

- (void)userTappedOnView:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint tapLocation = [recognizer locationInView:self.tableView];
        NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
        if (tapIndexPath) {
            [self tableView:self.tableView didSelectRowAtIndexPath:tapIndexPath];
        }
    }
}

如果你想对单元格的触摸做出反应,你必须让你的 tableView 的委托指向控制器。在 viewDidLoad 中做:

self.tableView.delegate = self;

然后实现方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
于 2015-04-29T12:04:13.453 回答
0

好的,不确定这是否是您要查找的内容,但我认为您最好还是通过内置方法处理 tableView 操作。使用自定义 UITableViewCells 并根据单元格的子类确定要做什么。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)

    if let addTableViewCell = selectedCell as? AddNewTableViewCell {
        insertNewObject(addTableViewCell)
    } else if let someOtherTableViewCell = selectedCell as? SomeOtherTableViewCell {
        // Do something else
    }
}

// Copied from the Xcode template
func insertNewObject(sender: AnyObject) {
    objects.insert(NSDate(), atIndex: 0)
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
于 2015-04-29T12:04:06.547 回答