【问题标题】:DidSelectRowAtIndexPath not being called at all [duplicate]根本没有调用 DidSelectRowAtIndexPath [重复]
【发布时间】:2016-10-05 07:16:06
【问题描述】:

我已经设置了 tableview.delegate = self...从甜甜圈拖到委托和数据源...照顾协议和其他一切但是当我选择一个单元格时它不会被调用。我放了一个断点并打印,但什么也没发生

导入 UIKit

class StartCompetitionViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {



@IBOutlet weak var mainStackView: UIStackView!

@IBOutlet weak var pickThemeLabel: UILabel!

@IBOutlet weak var themeTextField: UITextField!
@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var middleStackView: UIStackView!
@IBOutlet weak var logoStackView: UIStackView!
@IBOutlet weak var buttonStackView: UIStackView!

@IBOutlet weak var leftOrTopButton: UIButton!
@IBOutlet weak var rightOrBottomButton: UIButton!


var autoComplete = [String]()

override func viewDidLoad() {
    super.viewDidLoad()




    leftOrTopButton.landingPageButtonsRound()
    leftOrTopButton.setTitle("Back", forState: .Normal)
    leftOrTopButton.backgroundColor = UIColor(red: 239.0/255.0, green: 77.0/255.0, blue: 77.0/255.0, alpha: 1.0)

    rightOrBottomButton.landingPageButtonsRound()
    rightOrBottomButton.setTitle("Next", forState: .Normal)
    rightOrBottomButton.backgroundColor = UIColor(red: 249.0/255.0, green: 214.0/255.0, blue: 0.0/155.0, alpha: 1.0)

    pickThemeLabel.backgroundColor = UIColor(red: 25.0/255.0, green: 25.0/255.0, blue: 25.0/255.0, alpha: 1.0)
    pickThemeLabel.text = "Pick a Theme"
    pickThemeLabel.textColor = UIColor.whiteColor()


    view.backgroundColor = UIColor(red: 25.0/255.0, green: 25.0/255.0, blue: 25.0/255.0, alpha: 1.0)


    themeTextField.keyboardAppearance = .Dark
    themeTextField.autocorrectionType = .No



    leftOrTopButton.setTitle("Back", forState: .Normal)

    rightOrBottomButton.setTitle("Next", forState: .Normal)

    slowFadeInMiddleInformation()


    themeTextField.delegate = self
    themeTextField.backgroundColor = UIColor.whiteColor()

// tableView.delegate = self

    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None



    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(StartCompetitionViewController.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(StartCompetitionViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil);


    hideKeyboardWhenTappedAround()


}


@objc func keyboardWillShow(sender: NSNotification) {

    view.frame.origin.y = -170

}
@objc func keyboardWillHide(sender: NSNotification) {

    view.frame.origin.y = 0

}

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


func textFieldShouldEndEditing(textField: UITextField) -> Bool {

    self.themeTextField.resignFirstResponder()

    return true
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

func slowFadeInMiddleInformation(){

    self.middleStackView.alpha = 0.0

    UIView.animateWithDuration(0.5,
                               delay: 0.0,
                               options: .CurveLinear,
                               animations: {


                                self.middleStackView.alpha = 1.0




        }, completion: nil)

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    let index = indexPath.row as Int

    cell.textLabel!.text = autoComplete[index]



    cell.textLabel!.font = UIFont(name:"Helvetica", size: 24)
    cell.textLabel?.textAlignment = .Center
    cell.backgroundColor = UIColor(red: 25.0/255.0, green: 25.0/255.0, blue: 25.0/255.0, alpha: 1.0)
    cell.textLabel?.textColor = UIColor.whiteColor()


    return cell

}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    let substring = (textField.text!.lowercaseString as NSString).stringByReplacingCharactersInRange(range, withString: string)

    substring.lowercaseString

    searchAutocompleteEntriesWithSubstring(substring.lowercaseString)


    if textField == themeTextField{
        _ = range.length == 0 ? textField.text! + string : (themeTextField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)



    }

    return true

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return autoComplete.count

}

func searchAutocompleteEntriesWithSubstring(substring: String) {

    autoComplete.removeAll(keepCapacity: false)

    for key in LocalModal().possibleThemesArray {

        let myString: NSString! = key.lowercaseString as NSString

        let substringRange :NSRange! = myString.rangeOfString(substring)

        if (substringRange.location  == 0) {
            autoComplete.append(key)
        }

    }

    tableView.reloadData()

}




func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    let selectedCell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

    themeTextField.text = selectedCell.textLabel!.text!

    autoComplete.removeAll(keepCapacity: false)

    tableView.reloadData()

   print("foo")
}



func animateEverythingBack(){

    UIView.animateWithDuration(1.5,
                               delay: 0.0,
                               options: .CurveLinear,
                               animations: {



                                self.middleStackView.center.y += self.view.bounds.width


                                self.buttonStackView.center.y += self.view.bounds.width






        }, completion: nil)

}

func fadeOutOfMiddleStackView(){

    UIView.animateWithDuration(0.5,
                               delay: 0.0,
                               options: .CurveLinear,
                               animations: {

                                self.middleStackView.alpha = 0.0

        }, completion: nil)

}



@IBAction func leftOrTopButtonTapped(sender: UIButton) {


    animateEverythingBack()
    fadeOutOfMiddleStackView()

    delay(1.6) { 
        self.performSegueWithIdentifier("fromStartToLanding", sender: self)
    }



}

@IBAction func bottomOrRightButtonTapped(sender: UIButton) {

    self.performSegueWithIdentifier("fromStartToWillJoin", sender: self)

}

}

【问题讨论】:

  • 1) 删除代码中的 tableView.delegate = self (如果在情节提要中连线,则不需要)。 2) 确保在 viewContorller 中声明了 UITableViewDatasSource 和 UITableViewDelegate 协议。在主声明中,或作为扩展(首选)。
  • @DJohnson 谢谢,我删除了不需要的 tableView.delegate = self 并且我有协议但它仍然没有响应
  • 你能发布整个视图控制器代码,以及故事板中代理接线的屏幕截图吗?问题可能出在这两个地方之一。
  • @DJohnson 我添加了完整代码和屏幕截图

标签: swift cocoa-touch


【解决方案1】:

我将您的代码提取到 XCode 中(在一个空白项目中)。

我注释掉了所有与 tableView 无关的行。
我将一个 tableView 放到视图控制器中,分配委托和数据源给原型单元格“单元格”的标识符,它可以工作,这意味着 tableview 被绘制,当我选择一行时,它会触发 didSelectRowAtIndexPath。

您发送了 viewController 插座的屏幕截图,但问题可能出在 tableView 插座上。将它们全部删除并仅添加 dataSource 和委托(用于调试目的)

此外,cellForRowAtIndexPath 和 didSelect... 中的代码...您正在将值分配给它没有引用的项目(假设您使用的是原型 UITableViewCell 而不是自定义单元格?)对于测试,我会注释掉cellForRow 中的所有内容...除了将单元格出列并返回单元格以及在 didSelect 中...暂时离开 print("foo")。

另外,我只是给 autoComplete 一个 5 条记录的值,我将有 5 行。我假设您正在获取行,因此自动完成不是问题。

如果这有帮助,请告诉我。如果需要,很乐意提供进一步帮助。

【讨论】:

  • 我删除了 tableview 并添加了一个新的和一个新的单元格...评论了你所说的所有内容我仍然一无所获...我能做的一件事就是上下滚动列表...所以表格视图正在工作,而不是选择
  • 如果您想将整个项目发布到 github 或发送给我,我很乐意查看。
  • 我取得了突破……我可以在多次单击后选择一个……并尝试双击……这是否说明问题?
  • 你一直都是对的,谢谢
猜你喜欢
  • 2017-11-12
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2012-04-06
相关资源
最近更新 更多