从解析数组获取表格视图(Swift 2)

问题描述:

我想从Parse中的“用户”类中的“my_classes”中的字符串中抽取数组。当我点击搜索按钮时,我希望数组中的每个单独的字符串成为tableview中的单独单元格。这是我在“my_classes”中的数组:[“Physics”,“Economics”,“Pre Calculus”]。我想 “物理学”,因为它是自己的细胞, “经济学” 作为自己的电池等从解析数组获取表格视图(Swift 2)


import UIKit 
import Parse 

class CardSetClassTableViewController: UITableViewController, UISearchBarDelegate { 

    // MARK: Outlets 

    @IBOutlet var searchBar: UISearchBar! 

    @IBOutlet var resultsTableView: UITableView! 


    // MARK: Variables 

    var searchResults = [String]() 


    // MARK: Actions 

    @IBAction func newClassBarButtonItemPressed(sender: AnyObject) { 

     self.performSegueWithIdentifier("newClassSegue", sender: self) 

    } 



    // MARK: Functions 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.searchBar.delegate = self 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func displayAlert(title: String, message: String) { 

     let alert = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     self.presentViewController(alert, animated: true, completion: nil) 

    } 

    func searchBarSearchButtonClicked(searchBar: UISearchBar) 
    { 

     if reachabilityStatus == kNOTREACHABLE { 

      self.displayAlert("No Internet Connection", message: "Please connect to the internet before continuing.") 

     } else { 

      searchBar.resignFirstResponder() 
      print("Search word = \(searchBar.text!)") 

      let classNameQuery = PFQuery(className:"_User") 
      classNameQuery.whereKey("my_classes".lowercaseString, equalTo: searchBar.text!.lowercaseString) 


      let query = PFQuery.orQueryWithSubqueries([classNameQuery]) 



      query.findObjectsInBackgroundWithBlock { 
       (results: [PFObject]?, error: NSError?) -> Void in 

       if error != nil { 

        self.displayAlert("Error", message: error!.localizedDescription) 

        return 
       } 

       if let objects = results { 

        self.searchResults.removeAll(keepCapacity: false) 

        for object in objects { 

         let className = object.valueForKey("my_classes") as! String 


         self.searchResults.append(className) 


        } 

        dispatch_async(dispatch_get_main_queue()) { 
         self.resultsTableView.reloadData() 
         self.searchBar.resignFirstResponder() 

        } 



       } 




      } 



     } 

    } 


    // MARK: - Table view data source 



    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return searchResults.count 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 


     cell.textLabel!.text = searchResults[indexPath.row] 

     return cell 

    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 



     let classIndexPath = tableView.indexPathForSelectedRow! 

     let selectedCell = tableView.cellForRowAtIndexPath(classIndexPath)! as UITableViewCell 

     let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
     spinningActivity.labelText = "Loading" 



     if reachabilityStatus == kNOTREACHABLE { 

      spinningActivity.hide(true) 

      self.displayAlert("No Internet Connection", message: "Please connect to the internet before continuing.") 


     } else { 

      // let className : String = String(selectedCell.textLabel!.text!) 

      self.performSegueWithIdentifier("addCardSet", sender: self) 

     } 


     searchBar.resignFirstResponder() 
    } 

} 

谢谢!

请尝试以下...

编辑

var songsArray = [String]() 

func fetchUsers() { 
let userQuery: PFQuery = PFUser.query()! 

//search users by the sepcified username, returns a users! object 
//make an array to put the values from the users! array object into 
//then append those from your "middle-man" array into your destination array,  
//in this example songArray is destination array and songsFromParse is "middle-man" array 

userQuery.whereKey("username", equalTo: (username)!) 
userQuery.findObjectsInBackgroundWithBlock({ 
    (users, error) -> Void in 

    var songsFromParse = users! 

    if error == nil { 
     if songsFromParse.count != 0 { 

     self.songsArray = (songsFromParse[i].valueForKey("CurrentSongURLArray") as! Array) 
      } 


     self.tableView.reloadData() 
    } else { 
     print(error) 
    } 
}) 
} 

然后你把你的新数组包含您检索的对象,在这个例子中songsArray,并用它来填充的tableView。在的cellForRowAtIndexPath ...如果

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

let cell = tableView.dequeueReusableCellWithIdentifier("Cell ID") 
cell?.textLabel?.text = songsArray[indexPath] 
return cell! 
} 
+0

P.S此为你工作,请关闭并接受答案:) – MikeG

+1

它看起来像你试图让歌曲从每个用户的查询。我正试图从“my_classes”只从一个用户拉一串字符串。 –

+0

噢好吧,我看到了,我只是编辑了我的答案。现在你用一个指定的'username'进行搜索,然后通过指定它的'valueForKey'来检索你想要的'[Array]'',这就是你在Parse中命名的那个列 – MikeG