为什么扩展会阻止tableViewCells中的触摸事件?

时间:2016-06-20 10:29:30

标签: ios objective-c swift uitableview swift2

我的UITableView过去常常起作用,它停止了按照预期行事。我发现segue仍然可行,但不仅仅是触摸单元格,你需要按住按钮并滑动然后它会完成segue。单次触摸完成后不会调用委托方法。经过一段时间的研究,我认为最好的想法是在这里发布我的问题,因为查看stackoverflow,似乎没有人有类似的问题。

我可以点击商店,商店被选中,但我必须保持按下按钮,而不是很多时间,几毫秒,几乎立即。该行变为灰色但没有任何反应。如果我松开按钮,那么也没有任何反应。如果我滑动那么它会做segue。 我的代码中甚至在我的整个项目中都没有添加单个手势。我在我的代码中找到了一个扩展,它实际上为我父亲viewController自定义类添加了一个手势。在下面找到它

这是我的表格视图代码:

    //
//  showShopsTableView.swift
//  yuApp
//
//  Created by Alfredo on 16/5/16.
//  Copyright © 2016 Alfredo. All rights reserved.
//


import UIKit
import CoreLocation
import MapKit


class showShopsTableView : ViewController, UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate, UISearchResultsUpdating {
    var selectedRow : shopsTableViewCell!
    var index : Int = 0
    @IBOutlet var searchBar: UISearchBar!


    @IBOutlet weak var currentTableView: UITableView!

    let resultSearchController = UISearchController(searchResultsController:nil)

    var searchActive : Bool = false
    var filtered:[String] = []


    override func viewDidLoad(){

        self.currentTableView?.delegate = self;
        self.currentTableView?.dataSource = self;

        super.viewDidLoad()

        //Retrieve data asynchronously
        let call = webApi()
        call.retrieveOneCityShops{(success) in

            self.doTableRefresh()
        }

        viewDidLayoutSubviews()

    }

    override func viewDidLayoutSubviews() {
        /*
        if let rect = self.navigationController?.navigationBar.frame {
            let y = rect.size.height + rect.origin.y
            self.currentTableView.contentInset = UIEdgeInsetsMake( y, 0, 0, 0)
        }*/
    }

    //Segue Method
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destinationViewController = segue.destinationViewController as! detailViewShop

        sendVariablesToDetailView(destinationViewController)

    }
    //Set variables in the next view controller before do a segue
    func sendVariablesToDetailView(destinationViewController : detailViewShop){
        destinationViewController.dataSend = self.selectedRow
        destinationViewController.titleText = self.selectedRow.shopName.text
        destinationViewController.descriptionText = self.selectedRow.shopDescription.text
        destinationViewController.imageOutlet = self.selectedRow.shopImg
        destinationViewController.shopCategory = self.selectedRow.label1.text
        destinationViewController.brands = self.selectedRow.label2.text
    }

    //TableView Delegate Methods
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedRow = tableView.cellForRowAtIndexPath(indexPath)! as! shopsTableViewCell
        self.performSegueWithIdentifier("cell", sender: self)
    }


    func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int
    {
        /*
        if(searchActive) {
            return filtered.count
        }
        */
        return TableData.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : shopsTableViewCell = tableView.dequeueReusableCellWithIdentifier("shopCell", forIndexPath: indexPath) as! shopsTableViewCell
        cell.shopName.text = TableData[indexPath.row]
        cell.shopDescription.text = values[indexPath.row].address
        cell.label1.text = values[indexPath.row].city
        cell.label2.text = values[indexPath.row].distance + "Km"
        cell.shopImg = UIImageView.init(image: UIImage.init(named: "versace_shop.jpg"))
        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 125
    }
    //SearchBar Delegate Methods
    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchActive = true;
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        filtered = TableData.filter({ (text) -> Bool in
            let tmp: NSString = text
            let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
            return range.location != NSNotFound
        })
        if(filtered.count == 0){
            searchActive = false;
        } else {
            searchActive = true;
        }
        self.currentTableView.reloadData()
    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {

    }

    func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
    }
    func filterContentForSearchText(searchText: String, scope: String = "All") {
        currentTableView.reloadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func doTableRefresh(){
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.currentTableView.reloadData()
            return
        })
    }


}

编辑:删除所有显示属性userInteractionEnabled的图像为TRUE。我终于找到了问题的根源。它与storyBoard无关。是我为我的主要自定义类viewController编写的扩展名(也是此窗口的父级)。扩展名是:

import Foundation
import UIKit
// Put this piece of code anywhere you like
extension ViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboard() {
        view.endEditing(true)
    }
}

在我的自定义viewController课程的最开始,在super.viewDidLoad()之后调用它:

        override func viewDidLoad() {
        super.viewDidLoad()
//      Hide keyboard when tap away

        self.hideKeyboardWhenTappedAround()

我在super.viewDidLoad()课程中评论了showShopsTableView,现在工作得很好。 我不太清楚为什么这段代码会阻止我的tableView的触摸事件,所以如果有人想解释它,我会接受他的回答。如果您发现它有用,我也非常感谢我的问题。 无论如何,非常感谢所有花一分钟的人。

0 个答案:

没有答案