TableViewCell切换数据源swift后不再检测到长按

时间:2016-09-24 23:36:32

标签: ios swift

所以我很好奇为什么更改数据源会禁用swift中的长按。我目前正在使用XCode 8版本5.我还没有找到解决可能导致的问题的方法。 longpress是否需要了解其他来源或者我是否对其进行了更改?

以下代码

@IBOutlet var tableView: UITableView!
@IBOutlet var changeDataBtn: UIButton!
@IBOutlet var ChangeBtn: UIButton!
var longPressRecognizer:UILongPressGestureRecognizer!
var refresh: UIRefreshControl!
var testList:[String] = []
var altList:[String] = ["Test Alt Cell 1","Test Alt Cell 2","Test Alt Cell 3","Test Alt Cell 4"]


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return self.testList.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
    cell.textLabel?.text = testList[indexPath.row]
    return cell
}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var i = 1
    while i < 6{
        testList.append("Test Cell \(i)")
        i+=1
    }
    refresh = UIRefreshControl()
    refresh.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresh.addTarget(self, action: #selector(refreshData), for: UIControlEvents.valueChanged)
    tableView.addSubview(refresh)

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
    self.view.addGestureRecognizer(longPressRecognizer)

}
func refreshData() {
    // Code to refresh table view
    doInBackGround()
}

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
    if longPressGestureRecognizer.state == UIGestureRecognizerState.began {

        let touchPoint = longPressGestureRecognizer.location(in: self.view)
        if tableView.indexPathForRow(at: touchPoint) != nil {
            print("Long Press Detected")
            // your code here, get the row for the indexPath or do whatever you want
        }
    }
}


func doInBackGround(){
    testList.removeAll()
    DispatchQueue.global(qos: .background).async {
        print("Refreshing Data Source")
        var temp:[String] = []
        var i = 1
        while i < 9{
            temp.append("New Test Cell \(i)")
            i+=1
        }
        self.testList = temp

    }

    DispatchQueue.main.async {

        self.tableView.reloadData()
        self.refresh.endRefreshing()
    }
}


@IBAction func changeDataSrc(_ sender: AnyObject) {
    print("Changed Data Source")
    refresh.beginRefreshing()
    testList = altList
    tableView.reloadData()
    refresh.endRefreshing()
    //let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
    //self.view.addGestureRecognizer(longPressRecognizer)
}






override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

2 个答案:

答案 0 :(得分:1)

您是什么意思&#34;更改数据源?&#34;

我认为您可以更改为数据源提供信息的数组,但不会更改表格视图的 <script> var app = angular.module('myApp', ['ui.bootstrap']); app.controller('myController', function($scope, $http){ $scope.getAirport = function(inp){ return $http.get('https://raw.githubusercontent.com/vedvasa/airports/master/airports.json', { params: { name : inp, sensor : false } }).then(function(res){ var names = []; angular.forEach(response.data.records, function(item){ names.push(item.addedNames); }); return names; }); }; $scope.on_item_selected=function($item, $model, $label) { $scope.selected_item = $item; } }); </script> <div ng-app="myApp" ng-controller="myController"> <form> <input type="text" class="form-control" id="source" placeholder="Enter Airport Code or City Name" ng-model="asyncSelected" typeahead="name for name in getAirports($viewValue)" typeahead-loading="loadingAirports" typeahead-on-select="on_item_selected($item, $model, $label)"> </form> </div> 属性。

您的代码正在更改从后台提供dataSource的数组。这是一个坏主意,可能导致副作用,包括崩溃。您应该将作业移至tableView(_, cellForRowAt:)self.testList的调用内部,以便从主线程更改数组。我不知道这是否是您当前问题的原因,但它可能会导致一些问题。

编辑:

您的代码过去看起来像这样:

DispatchQueue.main.async

相反,这样做:

func doInBackGround(){
    testList.removeAll()
    DispatchQueue.global(qos: .background).async {
        print("Refreshing Data Source")
        var temp:[String] = []
        var i = 1
        while i < 9{
            temp.append("New Test Cell \(i)")
            i+=1
        }
        self.testList = temp
    }

    DispatchQueue.main.async {
        self.tableView.reloadData()
        self.refresh.endRefreshing()
    }
}

答案 1 :(得分:0)

修复程序正在删除longPress

中的所有内容

所以看起来像这样:

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) { print("Long Press Activated") }