【发布时间】:2016-10-05 07:16:06
【问题描述】:
我已经设置了 tableview.delegate = self...从甜甜圈拖到委托和数据源...照顾协议和其他一切但是当我选择一个单元格时它不会被调用。我放了一个断点并打印,但什么也没发生
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