在我的应用程序中,我试图显示俱乐部员工的信息,其中 Fist 表列出了俱乐部(来自 Parse 后端),一旦选择了俱乐部,它就会显示所有员工(及其个人资料图片)再次在该俱乐部工作的表格(clubdetailTableVC),最后,如果选择了一名员工,那么他的完整详细信息将与他的个人资料图像一起显示在 detailVC 中。
如果能够将俱乐部对象从第一个 clubTableVC 传递给 clubDetailVC,但不能再进一步了。这是我的完整代码。所有员工(PFUser)都保存在“用户”类中,俱乐部表有员工数组(用户名作为字符串)。
//ClubtableVC.swift
import UIKit
class ClubsTableVC: PFQueryTableViewController {
override init(style:UITableViewStyle, className: String!){
super.init(style: style, className: className)
self.parseClassName = "Club"
self.textKey = "clubName"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Club")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("clubCell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "clubCell")
}
if let clubName = object?["ClubName"] as? String {
cell?.textLabel?.text = clubName
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//var detailScene = segue.destinationViewController as! ClubDetailsViewController
var clubScene = segue.destinationViewController as! clubPromoterTableVC
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
//detailScene.currentObject = (objects?[row] as! PFObject)
clubScene.currentObject = (objects?[row] as! PFObject)
}
}
}
//clubManagerVC.swift
class clubManagerTableVC: PFQueryTableViewController {
var currentObject : PFObject?
var usernames = [String]()
var images = [UIImage]()
override init(style:UITableViewStyle, className: String!){
super.init(style: style, className: className)
self.parseClassName = "_User"
self.textKey = "fullname_lower"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func queryForUser() -> PFUser {
var query = PFUser(className: "Club")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("managerCell") as! PFTableViewCell!
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "managerCell")
if cell == nil {
var query = PFUser.query()!
query.whereKey("username", equalTo: currentObject!)// currentObject from 1st VC
//query.whereKey("username", equalTo: PFUser.currentUser()!.objectId!)
//query.whereKey("objectId", containedIn: PFUser.currentUser()?["accepted"] as! [String])
query.findObjectsInBackgroundWithBlock { (results, error) -> Void in
if let results = results {
for result in results as! [PFUser] {
self.usernames.append(result.username!)
let imageFile = result["picture"] as! PFFile
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error != nil {
print(error)
} else {
if let data = imageData {
self.images.append(UIImage(data: data)!)
self.tableView.reloadData()
}
}
}
}
self.tableView.reloadData()
}
}
}
cell.textLabel?.text = usernames[indexPath.row]
if images.count > indexPath.row {
cell.imageView?.image = images[indexPath.row]
}
if let clubName = object?["ClubName"] as? String {
cell?.textLabel?.text = clubName
}
return cell
}
}