根本没有调用DidSelectRowAtIndexPath

时间:2016-06-05 17:06:54

标签: swift cocoa-touch

我设置了tableview.delegate = self ...从甜甜圈拖到委托和数据源....处理协议和其他所有但是当我选择一个单元格时它不会被调用。我提出了一个断点,并打印出来但没有发生任何事情

enter image description here         导入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 个答案:

答案 0 :(得分:3)

我将您的代码拉入XCode(在一个空白项目中)。

我评论了与tableView没有关系的所有行 我将一个tableView放到了视图控制器中,分配了委托和数据源给了原型单元格标识符" cell"并且它有效,意味着绘制了tableview,当我选择一行时,它会触发didSelectRowAtIndexPath。

你发送了一个viewController出口的截图,但问题可能在tableView出口。将它们全部删除并仅添加dataSource和委托(用于调试目的)

此外,cellForRowAtIndexPath和didSelect中的代码......您正在为它没有引用的项目赋值(假设您使用的是原型UITableViewCell而不是自定义单元格?)为了测试,我会注释掉cellForRow中的所有内容...除了将单元格出列,并返回单元格,然后在didSelect中....暂时不要打印(" foo")。

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

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