简体   繁体   English

为什么无法快速识别我的tableview数据源?

[英]why are my tableview datasource isn't recognized in swift?

// It says ViewController doesn't conform protocol with the UITableViewDataSource //and there are all of errors with the self. //它说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
}

You are missing a closing brace after retrieveMessages , which leads to a cascade of other errors (eg all those subsequent functions are considered private functions within retrieveMessages ). 您在retrieveMessages之后缺少右括号,这会导致其他错误级联(例如,所有这些后续函数都被视为retrieveMessages私有函数)。

When I see these sorts of issues, I find it useful to select all of the code and re-indent it (eg press control + i or choose "Re-indent" from the "Structure" submenu on the "Editor" menu), and all of a sudden the misalignment of braces will jump out at you based upon the indentation of various functions and the like. 当我看到这些问题时,发现选择所有代码并重新缩进非常有用(例如,按Control + i或从“编辑器”菜单上的“结构”子菜单中选择“重新缩进”),突然之间,由于各种功能的缩进等原因,大括号的未对准会突然出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM