为什么我的tableview数据源在swift中无法识别?

时间:2015-03-16 17:43:47

标签: ios uitableview swift uiviewcontroller

//它说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
}

1 个答案:

答案 0 :(得分:0)

您在retrieveMessages之后缺少一个右大括号,这会导致一系列其他错误(例如,所有后续函数都被视为retrieveMessages中的私有函数)。

当我看到这些问题时,我发现选择所有代码并重新缩进它很有用(例如按 control + i 或选择&# 34;从"编辑器"菜单中的"结构"子菜单重新缩进"突然间,根据缩进,大括号的错位会跳出来各种功能等。