tableview单元格未单击

时间:2016-11-10 09:19:34

标签: ios swift

UITableViewCell子类:

class MenuDrawerTableViewCell: UITableViewCell {  
@IBOutlet weak var Label3: UILabel!
@IBOutlet weak var Image30: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}}

我有那堂课,现在我的viewController有一张桌子。

class OpenMenu: UIViewController, UITableViewDelegate, UITableViewDataSource  { 
@IBOutlet weak var tableV: UITableView!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
var selected = 0
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    print("prepare")
    var DestVC  = segue.destination as! CheckinController
    var indexPath: NSIndexPath = self.tableV.indexPathForSelectedRow! as NSIndexPath
    let menuItem = menuList[indexPath.row]
    DestVC.varView =  menuItem.index      
}
let newSwiftColor = UIColor(red: CGFloat(255), green: CGFloat(0), blue: CGFloat(238), alpha: CGFloat(1))
var menuList = [Menu]()   
func loadMenu() {
    let photo1 = UIImage(named: "checkin")!
    let menu1 = Menu(name: "Check In", photo: photo1, index: 20)!
    let photo2 = UIImage(named: "checkin")!
    let menu2 = Menu(name: "Add Store", photo: photo2, index: 10)!
    menuList += [menu1, menu2]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuList.count // your number of cell here
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {      
    let cellIdentifier = "MenuDrawerTableViewCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as! MenuDrawerTableViewCell      
    let menuItem = menuList[indexPath.row]        
    cell.Image30.image =  menuItem.photo
    cell.Label3.text = menuItem.name
    cell.Label3.numberOfLines = 1;        
    if(0 == selected)
    {
        selected = 1
        self.tableV.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)
    }       
    return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    // cell selected code here
     print("cell selected")
     print(indexPath)    
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuDrawerTableViewCell", for: indexPath as IndexPath) as UITableViewCell        
    return cell
}   
@IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
    super.viewDidLoad()
   loadMenu()
    self.tableV.delegate = self
    self.tableV.dataSource = self
    let size:CGFloat = 35.0 // 35.0 chosen arbitrarily
    let preferences = UserDefaults.standard
    let name1 = ""+preferences.string(forKey: "name")!
    label2.text = name1
    label1.text = String(name1[name1.startIndex])
    label2.textColor = UIColor.white
    label1.textColor = UIColor.white
    label1.textAlignment = NSTextAlignment.center
    label1.font = UIFont.systemFont(ofSize: 17)
    label1.bounds =  CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: size, height: size))
    label1.layer.cornerRadius = size / 2
    label1.layer.borderWidth = 3.0
    label1.layer.backgroundColor = newSwiftColor.cgColor
    label1.layer.borderColor = UIColor.black.cgColor        
}
override func viewDidAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

我想要的是检测桌面视图上的点击。但是我无法检测到任何东西。我应该选择打印细胞,但我什么都没得到。为什么点击功能没有被调用?

4 个答案:

答案 0 :(得分:3)

我认为您需要选择tableview,如下所示:

enter image description here

答案 1 :(得分:2)

您可以检查您的代码,以实现两种时间方法(cellForRowAtIndexPath和cellForRowAt)

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

}

答案 2 :(得分:1)

首先确保您已连接tableview和连接的委托和数据源。 请删除以下代码行:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuDrawerTableViewCell", for: indexPath as IndexPath) as UITableViewCell        
    return cell
}

已经实现了此方法。 希望这能解决你的问题。

答案 3 :(得分:0)

我遇到了这个问题,并注意到在我的viewDidLoad代码中,以某种方式将tableView的“ isEditing”设置为“ true” ...删除了固定的那行代码。