2

I have set the tableview.delegate = self...dragged from the donut to delegate and datasource....taken care of the protocol and everything else BUT when I pick a cell it doesn't get called. I put a break point and put in a print but nothing is happening

enter image description here import 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)

}

}

RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70
  • 1) remove the tableView.delegate = self in the code (if you wired it in the storyboard, this is not needed). 2) Make sure you are declaring the UITableViewDatasSource and UITableViewDelegate protocols in the viewContorller. Either in the main declaration, or as extensions (preferred). – DJohnson Jun 05 '16 at 17:18
  • @DJohnson thank you, i removed the unneeded tableView.delegate = self and I have the protocols and it still does not respond – RubberDucky4444 Jun 05 '16 at 17:26
  • Can you post the entire view controller code, and a screen shot of the delegate wiring in storyboard? The issue is likely to be in one of those 2 places. – DJohnson Jun 05 '16 at 17:29
  • @DJohnson i added the full code and a screen shot – RubberDucky4444 Jun 05 '16 at 17:36

1 Answers1

3

I pulled your code into XCode (in a blank project).

I commented out all the lines that didn't have to do with the tableView.
I dropped a tableView into the view controller, assigned the delegate and datasource gave the prototype cell the identifier of "cell" and it works, meaning the tableview is drawn, and when I select a row, it fires the didSelectRowAtIndexPath.

You sent gave a screenshot of the viewController outlets, but the problem is probably in the tableView outlets. Remove them all and add only dataSource and delegate back (for debugging purposes)

Also, the code in cellForRowAtIndexPath and didSelect... you are assigning values to items it doesn't have a reference to (assuming you are using the prototype UITableViewCell and not a custom cell?) For testing, I would comment out everything in cellForRow... except dequeueing the cell, and returning the cell, and in the didSelect.... just leave print("foo") for now.

Also, I just gave autoComplete a value of 5 records to I would have 5 rows. I'm assuming you are getting rows, so the autocomplete is not the issue.

Let me know if this helps. Happy to help further if needed.

DJohnson
  • 929
  • 4
  • 15
  • I deleted the tableview and added a new one and a new cell...commented everything you said out i still get nothing....the one thing i can do is scroll the list up and down...so the tableview is working just not the select – RubberDucky4444 Jun 05 '16 at 18:43
  • If you want to post the whole project to github or send to me I'm happy to look at it. – DJohnson Jun 05 '16 at 18:47
  • I've had a breakthrough....i as able to select one after clicking a large amount of times...and tried double clicking....does that shed light? – RubberDucky4444 Jun 05 '16 at 18:52
  • you were right all along, thank you – RubberDucky4444 Jun 05 '16 at 19:29