- 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被点击后应该执行的代码。
更多回答
网站公告
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

