简体   繁体   English

DidselectRow委托方法无法快速运行

[英]DidselectRow delegate method not working in swift

I have an alert in which I have used table view to show some data. 我有一个警报,其中我使用表视图显示了一些数据。

User can select any item from the table view data. 用户可以从表视图数据中选择任何项目。

Now when I'm trying to select any item in didSelectRow method it isn't selecting an item. 现在,当我尝试在didSelectRow方法中选择任何项目时,就没有选择任何项目。 I have used breakpoints also but it isn't catching the breakpoints also. 我也使用过断点,但是它也没有捕获断点。

I have given its delegates and datasources everything is fine but i'm confused why it isn't working? 我已经给它的委托人和数据源一切都很好,但是我很困惑为什么它不起作用?

My code is this: 我的代码是这样的:

extension YourOrdersViewController : UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return PreOrderService.instance.PreOrderModelInstance.count
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Hello")
        print(PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time)
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if tableView == orderTableView{
            print("Dishes")
        }
        else{
            orderTableView.cellForRow(at: indexPath)?.accessoryType = .none
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = orderTableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath) as!
    PreOrderTableViewCell

        cell.titleLbl.text = PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time
        print(cell.titleLbl.text!)
        cell.selectionStyle = .none
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.accessoryType = cell.isSelected ? .checkmark : .none
        return cell
    }
}

View Controller looks like this, enter image description here View Controller如下所示, 在此处输入图像描述

Check or you have set this value: 检查或您已设置此值: 在此处输入图片说明 If No Selection is set instead of Single Selection , the func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method will not be executed. 如果未设置No Selection而不是Single Selection ,则将不执行func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)方法。

Does it have any gesture recognizer on your UIViewControlller. 它在UIViewControlller上是否有任何手势识别器。 If it does and the gesture recognizer is above your table views on responder chain, it will take any single touch and leave nothing for your table views. 如果是这样,并且手势识别器位于响应者链上的表格视图之上,则只需触摸一下,就不会留下任何表格视图。

Whole problem in your code I think is: 我认为您代码中的整个问题是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    self.tableView.cellForRow(at: indexPath)?.accessoryType = .none

}

Make sure that the delegate and datasource are set. 确保已设置委托和数据源。 If you're creating the views via storyboard, make sure that you set the iboutlets for delegate/datasource. 如果要通过情节提要板创建视图,请确保为委托/数据源设置iboutlet。

Both of these lines mean the same thing: so remove the second one. 这两行含义相同:因此删除第二行。

cell.selectionStyle = .none
cell.selectionStyle = UITableViewCellSelectionStyle.none

在Main.storyboard中选择您的表视图,然后在“属性检查器”中将选择更改为单个选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM