如何使tableViewCell同时处理tap和longPress?

时间:2016-06-12 02:44:49

标签: ios swift swift2 uigesturerecognizer

我把它放在cellForRowAtIndexPath

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
cell.addGestureRecognizer(longPress)
longPress.cancelsTouchesInView = true
let tapPress = UITapGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleTapPress))
cell.addGestureRecognizer(tapPress)
tapPress.cancelsTouchesInView = true

并将这些(下面的代码)放在它之外,并完全删除didSelectRowAtIndexPath函数,而是使用indexPathForSelectedRow来获取刚刚选择的行用户。

func handleLongPress(sender: UILongPressGestureRecognizer){
    let index = tableView.indexPathForSelectedRow!
    doSomething(index)
}

func handleTapPress(sender: UITapGestureRecognizer){
    let index = tableView.indexPathForSelectedRow!
    doSomethingElse(index)
}

结果是indexPathForSelectedRow返回nil,但我确实选择了一行,并且我的代码中没有“deselectRowAtIndexPath”。

3 个答案:

答案 0 :(得分:12)

请勿将 app.controller('MainCtrl', ['$scope', 'filterFilter', 'ItemsService', function ($scope, filterFilter, ItemsService) { $scope.items = ItemsService.get($scope.items) $scope.addLink = function () { $scope.errortext = ""; if (!$scope.newItem) {return;} if ($scope.items.indexOf($scope.newItem) == -1) { $scope.items.push($scope.newItem); $scope.errortext = "Submitted"; $scope.items = ItemsService.put($scope.items) } else { $scope.errortext = "Link in the list"; } }; $scope.removeItem = function(item) { $scope.items.splice($scope.items.indexOf(item), 1); $scope.items = ItemsService.put($scope.items) $scope.resetFilters; }; 添加到UILongPressGestureRecognizer。将其添加到Cell

中的UITableView
viewDidLoad

通过

获取触摸的单元格索引
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
yourTableView.addGestureRecognizer(longPress)

而不是func handleLongPress(sender: UILongPressGestureRecognizer){ if longPressGestureRecognizer.state == UIGestureRecognizerState.Began { let touchPoint = longPressGestureRecognizer.locationInView(yourTableView) if let indexPath = yourTableView.indexPathForRowAtPoint(touchPoint) { // your code here, get the row for the indexPath or do whatever you want } } } 使用UITapGestureRecognizer是一种更好的方法

答案 1 :(得分:2)

更新Swift4:

在您的viewController类的public FTPClient ftpConnect(){ FTPClient ftp = new FTPClient(); int retries=3; int timeout=5000; URL url; try { url = new URL(REMOTE_URL); if(!url.getProtocol().equals("ftp")) throw new MalformedURLException(); } catch (MalformedURLException e1) { throw new SystemException("Invalid protocol"); } ftp.setConnectTimeout(timeout); ftp.setDataTimeout(timeout); for (int i = 1; i <= retries; i++) { try { ftp.connect(url.getHost(),url.getPort()>0?url.getPort():21); ftp.setSoTimeout(timeout); ftp.enterLocalPassiveMode(); ftp.setKeepAlive(true); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new ConnectException("FTP server refused connection"); } if(StringUtils.isNotBlank(url.getUserInfo())){ String[] arr=URLDecoder.decode(url.getUserInfo(), "UTF-8").split(":"); String user=arr[0]; String pass=arr.length>0?arr[1]:""; if (!ftp.login(user,pass)) { ftp.logout(); throw new SecurityException("FTP server login not valid"); } } ftp.changeWorkingDirectory(url.getPath()); break; } catch (SocketException e ) { log.error("Socket Error, retry nr {}",i); if(i==retries) throw new SystemException("Socket error, max retries reached"); } catch (IOException e) { throw new SystemException("Connection Error"); } catch (SecurityException e) { throw new SystemException("Invalid authentication"); } } return ftp; } 中添加这些行(在此示例中,类的名称为viewDidLoad

YourViewController

现在将此override func viewDidLoad() { super.viewDidLoad() let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(longPressGestureRecognizer:))) self.view.addGestureRecognizer(longPressRecognizer) } 添加到您的viewController类中

func

答案 2 :(得分:1)

基于Bala答案是快速4或5

override func viewDidLoad() {
        super.viewDidLoad()
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
        tableView.addGestureRecognizer(longPress)
    }

这是方法

@objc func longPress(sender: UILongPressGestureRecognizer) {

            if sender.state == UIGestureRecognizer.State.began {
                let touchPoint = sender.location(in: tableView)
                if let indexPath = tableView.indexPathForRow(at: touchPoint) {
                    // your code here, get the row for the indexPath or do whatever you want
                    print("Long press Pressed:)")
                }
            }


        }