我有两个表视图,一个包含用户,另一个包含朋友。 我如何在这两者之间建立联系?例如,如果我按下该特定用户的按钮,它会将用户添加到我的朋友列表中,并存储在电话而不是Parse上。
import UIKit
import Parse
import Foundation
class AddFriendViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet var myFriendSearchBar: UISearchBar!
@IBOutlet var myFriendSearchList: UITableView!
var refreshControl = UIRefreshControl()
var searchResult = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Nexa Rust Script L", size: 25)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
myFriendSearchBar.delegate = self
myFriendSearchList.delegate = self
myFriendSearchList.dataSource = self
self.navigationItem.title = "Haffla"
navigationController?.setNavigationBarHidden(false, animated: true)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func returnDiss(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return searchResult.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! TableViewCell1
myCell.myLabel.text = searchResult[indexPath.row]
return myCell
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
myFriendSearchBar.resignFirstResponder()
let userName:PFQuery = PFUser.query()!
userName.whereKey("username", containsString: searchBar.text)
userName.limit = 1
userName.orderByAscending("username")
userName.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error: NSError?) -> Void in
if error != nil {
let myAlert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
if let friends = objects as [PFObject]? {
self.searchResult.removeAll(keepCapacity: false)
for object in friends {
let username = object.objectForKey("username") as! String
self.searchResult.append(username)
}
dispatch_async(dispatch_get_main_queue()) {
self.myFriendSearchList.reloadData()
self.myFriendSearchBar.resignFirstResponder()
}
}
}
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
myFriendSearchBar.resignFirstResponder()
myFriendSearchBar.text? = ""
}
}
编辑1:添加了TableView的tableview代码,您可以搜索该数据库中的人员。