【问题标题】:Swift: Delay in UITableViewCell SelectionSwift:UITableViewCell 选择中的延迟
【发布时间】:2015-09-10 01:07:32
【问题描述】:

我在另一个视图中有一个 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
    }
}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

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

    【讨论】:

    • 啊,是的,那会做到的:P ...很高兴你想通了!
    • 感谢您的跟进!这也是我的问题。我有一个点击手势来关闭键盘。一旦我评论说点击单元格就像一个冠军。
    【解决方案2】:

    这应该可行,但创建自定义单元子类会更简洁。

     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")
    
        cell.textLabel!.text = "Test"
    
        let backgroundView = UIView()
        backgroundView.backgroundColor = YOUR_COLOR
        cell.selectedBackgroundView = backgroundView
    
        return cell
    }
    

    删除,因为我们想要选择单元格

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    

    删除,因为这已经在单元子类中处理了

    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()
    }
    

    【讨论】:

    • 我目前使用的是默认单元格,而不是自定义单元格。这需要自定义单元格吗?另外,也许我读错了,但看起来这会在创建单元格后立即改变单元格的颜色。这不会在通过单击单元格选择单元格时更改它吗?
    • 这将设置单元格的“selectedBackgroundVIew”而不是“backgroundView”,因此它仅在突出显示和选择时出现...我将编辑我的答案以帮助您
    • 确定,您是正确的,它是它在选择单元格时会更改颜色,但我仍然在使用DidselectRoyatIndexpAth时遇到了同样的问题。我必须按住单元格 3 秒钟才能使单元格保持突出显示。
    • 您是否删除/注释掉了 didHighlightRowAtIndexPath 和 didUnhighlightRowAtIndexPath 以及 didSelectRowAtIndexPath 的所有代码? .... 单元格选择本身会与所有子视图的背景颜色混淆,因此可能会混淆它
    • 是的,已经被注释掉了。
    【解决方案3】:

    我没有足够的声望点来回复@Jason247,因此输入它作为答案。他是对的。我遇到了相同的问题,即单元格注册点击的延迟。注释掉 UITapGestureRecognizer 并且延迟消失了。

    在我的上下文中,我使用的是 UISearchBar。我用搜索栏的可选委托方法替换了我的 UITapGestureRecognizer:

    class ViewController: UIViewController, UISearchBarDelegate{
    
        func searchBarSearchButtonClicked(searchBar: UISearchBar)
        {
            self.mySearchBar.endEditing(true)
        }
    }
    

    您可以找到更多使用 UISearchBar here 时关闭键盘的解决方案

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 2019-10-26
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多