【问题标题】:UiTableView didSelectRowAt method not called未调用 UiTableView didSelectRowAt 方法
【发布时间】:2017-10-05 08:23:01
【问题描述】:

我正在为 UiTableView 实现滑动以删除操作,我创建了一个工作正常的表。但是当我点击表格视图中的项目时,didSelectRowAt 没有被调用 - 它没有打印日志。

代码

class ManageUsersTable: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var editView: UIView!
    @IBOutlet weak var main_user_table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        editView.isHidden = true


    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("UsersTableCell", owner: self, options: nil)?.first as! UsersTableCell
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("selected : \(indexPath.row)")
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        print("Delete at index : \(indexPath.row)")
        let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
            // delete item at indexPath
            print("Delete at index : \(indexPath.row)")
             self.editView.isHidden = false
        }
//        let edir = UITableViewRowAction(style: .normal, title: "Edit", icon : UIImage() ) { (action, indexPath) in
//            print("Edit at index : \(indexPath.row)")
//        }
//        edit.backgroundColor = UIColor.darkGray
//        return [delete, edit]
        return [delete]
    }
    } 

谁能帮我修复这个 tnx。

更新

当我从左向右滑动时,该方法有效。不是在项目上点击

【问题讨论】:

  • 您是否在其故事板/笔尖中将 tableview 委托设置为 ManageUsersTable?
  • 是的。我已经设置了委托
  • 验证userInteractionEnabled属性一次。
  • 你确定你的tableview选择是单选的吗?和编辑呢
  • 你为编辑设置了什么?

标签: ios uitableview swift3


【解决方案1】:

检查以下列表。

1) 不要忘记设置 UITableViewDelegate 或 UITableViewDataSource

2) 请更新您的 viewDidLoad 为

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        editView.isHidden = true

       main_user_table.allowsMultipleSelectionDuringEditing = false   //UPDATE THIS LINE
    }

3) 删除滑动的默认行为如下

a) 当您滑动单元格时 -> 您会看到一个删除按钮。

这个函数被调用

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        print("swipe")
    return true
}

b) 当你点击删除按钮时,这个函数被调用

  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if(editingStyle == UITableViewCellEditingStyle.delete)
        {
            print("Delete Pressed")

        }
        }

c) 现在,当您的删除按钮可见,然后您点击单元格而不是删除按钮本身时,删除按钮将隐藏,并且第一次不会调用任何方法。

现在删除按钮被隐藏,您只需点击单元格。好了,现在你的下面的函数被调用了

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt(\(indexPath)")
    }

除此之外,如果您想要任何其他行为。然后你必须自定义 UITableViewCell。

如果您遇到任何其他问题,请彻底重新检查您的实施。

【讨论】:

    【解决方案2】:

    我找到了解决办法

    在您的 viewDidLoad 中执行此操作

     let tapOnScreen: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ManageDevicestable.CheckTheTime))
            tapOnScreen.cancelsTouchesInView = false
            managetable.addGestureRecognizer(tapOnScreen)
    

    添加选择器方法

     func CheckTheTime() -> Void {
            print("tapped")
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多