ios - Swift:延迟 UITableViewCell 选择

标签 ios swift uitableview

我在另一个 View 中有一个 UITableView。每当我单击表格中的一个单元格时,我希望该单元格突出显示,直到再次单击它以取消选择它。使用 didSelectRowAtIndexPath 方法,我已经完成了这个。但是,小区选择需要很长时间。我必须按住一个单元格 3 秒钟才能突出显示该单元格,而不是瞬间。如何让它在触摸时立即选择单元格?

这是我的相关代码。

class AddDataViewController : UIViewController {

    @IBOutlet weak var locationTableView: UITableView!  

    var fstViewController : FirstViewController?

    let locationTableViewController = LocationTableViewController()

    override func viewWillAppear(animated: Bool) {
        // Set the data source and delgate for the tables
        self.locationTableView.delegate = self.locationTableViewController
        self.locationTableView.dataSource = self.locationTableViewController

        // Set the cell separator style of the tables to none
        self.locationTableView.separatorStyle = UITableViewCellSeparatorStyle.None

        // Refresh the table
        self.locationTableView.reloadData()
    }

    override func viewDidLoad(){
        super.viewDidLoad()

        // Create a tap gesture recognizer for dismissing the keyboard
        let tapRecognizer = UITapGestureRecognizer()

        // Set the action of the tap gesture recognizer
        tapRecognizer.addTarget(self, action: "dismissKeyboard")

        // Add the tap gesture recognizer to the view
        //self.view.addGestureRecognizer(tapRecognizer)
    }
}

class LocationTableViewController : UITableViewController, UITableViewDelegate, UITableViewDataSource {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return tableView.frame.height / 5
    }

    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.greenColor()
    }

    override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.blueColor()
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")

        cell.selectionStyle = UITableViewCellSelectionStyle.None

        cell.textLabel!.text = "Test"

        return cell
    }
}

最佳答案

问题是 UITapGestureRecognizer 干扰了单元格的点击。我很抱歉点击手势代码不在我最初的帖子中,因为我没有意识到这可能是罪魁祸首。我已将其添加到原始帖子的代码片段中。

关于ios - Swift:延迟 UITableViewCell 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016244/

相关文章:

ios - 为什么在 updateConstraints 中更改约束比其他时候更快?

ios - 如何将数组保存为iPhone中的字符串?

objective-c - NSJSONSerialization 不创建可变容器

iOS - UITableView - 快速滚动时行神秘地重复自己? (模拟器)

ios - 如何更改表格 View 单元格的选定颜色?

ios - 检测不到区域内所有Estimotes时如何在didExitRegion中添加条件

ios - UITapGestureRecognizer 在 UIImageView 上工作,但不在 UILabel 上工作

swift - 了解 Realm 对象服务器上的用户

ios - 添加新的自定义 View Controller 后应用程序无法运行

ios - UITableView 部分索引不更新