账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    uitableview点击cell高亮但是不执行didSelectRowAt indexPath
    24
    0

    代码如下

    点击可以高亮显示,但是不走didSelectRowAt indexPath的方法。

    //
    //  menuViewController.swift
    //  record
    //
    //  Created by 郭 on 2018/5/26.
    //  Copyright © 2018年 郭. All rights reserved.
    //
    
    import UIKit
    
    class MenuViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate{
        var table:UITableView!
        let bgColor = UIColor.init(red: 45/255, green: 50/255, blue: 53/255, alpha: 1)
        let arry:[String] = ["列表", "2", "3"]
        var changeView:changeViewDelegate? = nil
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = bgColor
            self.setUpLayout()
            // Do any additional setup after loading the view.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        func setUpLayout(){
            let rect = self.view.frame
            self.table = UITableView(frame: CGRect(x: 0, y: 63, width: rect.width, height: rect.height-62))
            self.table.backgroundColor = bgColor
            self.table.dataSource = self
            self.table.separatorStyle = UITableViewCellSeparatorStyle.none
            self.table.rowHeight = 100
            self.table.allowsSelection = true;
            self.view.addSubview(self.table)
            
        }
    
        func  rowChangeView() {
            print("adf")
            
        }
        //    列表代理方法
        
        //设置cell的数量
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return arry.count
        }
        
        //设置section的数量
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }
        public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
            print("indexPath.row = SelectRow第\(indexPath.row)行")
        }
        //设置tableview的cell
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let rect = self.view.frame
            let cell = UITableViewCell.init(frame: CGRect(x: 0, y: 0, width: rect.width, height: 100))
            cell.backgroundColor = bgColor
            let indexLabel = UILabel.init(frame: CGRect(x: 23, y: 16, width: rect.width, height: 20))
            indexLabel.textColor  = UIColor.white
            indexLabel.text = arry[indexPath.row]
            cell.addSubview(indexLabel)
            
            return cell
        }
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 云顶红尘客 普通会员 1楼

      在UitableView中点击cell时,如果点击的是cell的子视图,而该子视图没有在didSelectRowAt方法中被调用,那么点击cell后并不能执行didSelectRowAt方法中的代码。

      这是因为cell和其子视图之间的关联并不直接,而是在UINavigationController或UIViewController的视图控制器的代理方法中进行关联的。因此,只有当某个视图控制器的代理方法被调用时,才能触发cell的didSelectRowAt方法。

      如果你的cell和其子视图之间有直接的关联,那么你应该在cell的代理方法中调用didSelectRowAt方法,而不是在视图控制器的代理方法中。

      以下是一个示例:

      ```swift class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

      var tableView: UITableView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
      
          tableView.delegate = self
          tableView.dataSource = self
          tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print("cell \(indexPath.row) was selected")
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 10
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
          cell.textLabel?.text = "\(indexPath.row + 1)"
          return cell
      }
      

      } ```

      在这个示例中,cell的代理方法中调用了didSelectRowAt方法,并打印出了cell的索引值,这是cell被点击后应该执行的代码。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部