为什么我的 tableview 数据源在 swift 中无法识别?
why are my tableview datasource isn't recognized in swift?
// 它说 ViewController 不符合 UITableViewDataSource 的协议
//并且自我存在所有错误。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITextFieldDelegate, UITableViewDataSource {
@IBOutlet weak var dockViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var messageTableView: UITableView!
@IBOutlet weak var messageTextField: UITextField!
@IBOutlet weak var messageSendButton: UIButton!
var messagesArray:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.messageTableView.delegate = self
self.messageTableView.dataSource = self
//set self as delagate for the textfield
self.messageTextField.delegate = self
// add a recognizer tapped to the tableview
let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tableViewTapped")
self.messageTableView.addGestureRecognizer(tapGesture)
// retrieve messages from parse
self.retrieveMessages()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func sendButtonTapped(sender: UIButton) {
//sendButton is tapped
// call the end editing method for the textfield
self.messageTextField.endEditing(true)
// disable the send button and textfield
self.messageTextField.enabled = false
self.messageSendButton.enabled = false
// create a PFObject
var newMessageObject : PFObject = PFObject(className: "Message")
// set the Text key(Parse) to the messagetextfield
newMessageObject["Text"] = self.messageTextField.text
// save the pfobject
newMessageObject.saveInBackgroundWithBlock { (success: Bool, error: NSError!) -> Void in
if (success == true){
//message has been saved
NSLog("Message Saved Successfully")
// todo: retieve the lastest messages and reload the table
}
else{
// something went wrong
NSLog(error.description)
}
// enable the send button and textfeild
self.messageSendButton.enabled = true
self.messageTextField.enabled = true
self.messageTextField.text = ""
}
}
func retrieveMessages(){
// create new PFQuery
var query: PFQuery = PFQuery(className: "Message")
// find objects in the background
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error: NSError!) -> Void in
// clear the messages array
self.messagesArray = [String]()
//loop through the objects array
for messageObject in objects {
// retrieve the text column value of each pfobject
let messageText : String? = (messageObject as PFObject) ["Text"] as? String
// assign pfobject to the messagesArray
if messageText != nil {
self.messagesArray.append(messageText!)
}
}
// reload the tableview
self.messageTableView.reloadData()
}
func tableViewTapped(){
// force the textfield to end editing
self.messageTextField.endEditing(true)
}
// MARK: Textfield Delagate Methods
func textFieldDidBeginEditing(textField: UITextField) {
//perform an animation to expand the dockview
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {
self.dockViewHeightConstraint.constant = 351
self.view.layoutIfNeeded()
}, completion: nil)
}
func textFieldDidEndEditing(textField: UITextField) {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {
self.dockViewHeightConstraint.constant = 60
self.view.layoutIfNeeded()
}, completion: nil)
}
// MARK: TableView Delagate Methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Create a TableView Cell
let cell = self.messageTableView.dequeueReusableCellWithIdentifier("MessageCell") as UITableViewCell
// Customize the cell
cell.textLabel?.text = self.messagesArray[indexPath.row]
//return that Cell
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.messagesArray.count
}
您在 retrieveMessages
之后缺少右大括号,这会导致一连串其他错误(例如,所有这些后续函数都被视为 retrieveMessages
中的私有函数)。
当我看到这类问题时,我发现 select 所有代码并重新缩进它很有用(例如按 control+i 或从 "Editor" 菜单的 "Structure" 子菜单中选择 "Re-indent"),突然之间,根据各种函数的缩进等。
// 它说 ViewController 不符合 UITableViewDataSource 的协议 //并且自我存在所有错误。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITextFieldDelegate, UITableViewDataSource {
@IBOutlet weak var dockViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var messageTableView: UITableView!
@IBOutlet weak var messageTextField: UITextField!
@IBOutlet weak var messageSendButton: UIButton!
var messagesArray:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.messageTableView.delegate = self
self.messageTableView.dataSource = self
//set self as delagate for the textfield
self.messageTextField.delegate = self
// add a recognizer tapped to the tableview
let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tableViewTapped")
self.messageTableView.addGestureRecognizer(tapGesture)
// retrieve messages from parse
self.retrieveMessages()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func sendButtonTapped(sender: UIButton) {
//sendButton is tapped
// call the end editing method for the textfield
self.messageTextField.endEditing(true)
// disable the send button and textfield
self.messageTextField.enabled = false
self.messageSendButton.enabled = false
// create a PFObject
var newMessageObject : PFObject = PFObject(className: "Message")
// set the Text key(Parse) to the messagetextfield
newMessageObject["Text"] = self.messageTextField.text
// save the pfobject
newMessageObject.saveInBackgroundWithBlock { (success: Bool, error: NSError!) -> Void in
if (success == true){
//message has been saved
NSLog("Message Saved Successfully")
// todo: retieve the lastest messages and reload the table
}
else{
// something went wrong
NSLog(error.description)
}
// enable the send button and textfeild
self.messageSendButton.enabled = true
self.messageTextField.enabled = true
self.messageTextField.text = ""
}
}
func retrieveMessages(){
// create new PFQuery
var query: PFQuery = PFQuery(className: "Message")
// find objects in the background
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error: NSError!) -> Void in
// clear the messages array
self.messagesArray = [String]()
//loop through the objects array
for messageObject in objects {
// retrieve the text column value of each pfobject
let messageText : String? = (messageObject as PFObject) ["Text"] as? String
// assign pfobject to the messagesArray
if messageText != nil {
self.messagesArray.append(messageText!)
}
}
// reload the tableview
self.messageTableView.reloadData()
}
func tableViewTapped(){
// force the textfield to end editing
self.messageTextField.endEditing(true)
}
// MARK: Textfield Delagate Methods
func textFieldDidBeginEditing(textField: UITextField) {
//perform an animation to expand the dockview
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {
self.dockViewHeightConstraint.constant = 351
self.view.layoutIfNeeded()
}, completion: nil)
}
func textFieldDidEndEditing(textField: UITextField) {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {
self.dockViewHeightConstraint.constant = 60
self.view.layoutIfNeeded()
}, completion: nil)
}
// MARK: TableView Delagate Methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Create a TableView Cell
let cell = self.messageTableView.dequeueReusableCellWithIdentifier("MessageCell") as UITableViewCell
// Customize the cell
cell.textLabel?.text = self.messagesArray[indexPath.row]
//return that Cell
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.messagesArray.count
}
您在 retrieveMessages
之后缺少右大括号,这会导致一连串其他错误(例如,所有这些后续函数都被视为 retrieveMessages
中的私有函数)。
当我看到这类问题时,我发现 select 所有代码并重新缩进它很有用(例如按 control+i 或从 "Editor" 菜单的 "Structure" 子菜单中选择 "Re-indent"),突然之间,根据各种函数的缩进等。