如何检测UITableView清晰部分的点击? [英] How to detect tap on clear part of UITableView?

查看:101
本文介绍了如何检测UITableView清晰部分的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UITableView的视图控制器。表格视图的高度是静态的,==屏幕的高度。表中的行数可以更改,有时可能是行数小于可见行数的情况,因此在表底部会出现一些清晰区域。我可以在没有手势识别器的情况下检测到它吗?是否有一些UITableView的委托方法?

I have view controller with UITableView. Height of table view is static and == height of screen. Number of rows in table can change and sometimes it might be a situation when number of rows is less than number of rows which can be visible, so appears some clear area on bottom of table. Can i detect tap on it without gesture recognizer? Are there some delegate methods of UITableView?

推荐答案

是的,有委托方法,例如:

Yes, there are delegate methods, such as:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

但是,这只会告诉您是否在现有行上发生了点击。如果要捕获行(或部分标题)下方空白区域的点击,则需要使用手势识别器。你可以这样做:

However, this will only tell you if a tap occurs on an existing row. If you want to capture taps on the empty space below the rows (or on a section header) you will need to use a gesture recognizer. You can do something like this:

// in viewDidLoad or somewhere similar
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableTapped:)];
[self.tableView addGestureRecognizer:tap];
//.........

- (void)tableTapped:(UITapGestureRecognizer *)tap
{
    CGPoint location = [tap locationInView:self.tableView];
    NSIndexPath *path = [self.tableView indexPathForRowAtPoint:location];

    if(path)
    {
        // tap was on existing row, so pass it to the delegate method
        [self tableView:self.tableView didSelectRowAtIndexPath:path];
    }
    else
    {
        // handle tap on empty space below existing rows however you want
    }
}






编辑:对于替代方法,请考虑 Connor Neville的方法来自他对这篇文章的回答,并将手势识别器添加到桌子的背景中。


for an alternative approach, consider Connor Neville's approach from his answer on this post and add the gesture recognizer to the table's background.

这篇关于如何检测UITableView清晰部分的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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