以下是我的代码。在对应用程序进行测试后,我意识到didSelectRowAtIndex
是在prepareForSegue
之后运行的。如何让didSelectRowAtIndex
先运行。
如果答案涉及线程,我不知道它是如何工作的,所以请解释一下。感谢信。
import UIKit
class AvailableShifts: UITableViewController {
var shiftData: NSMutableArray = NSMutableArray()
var shiftID: String!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
// self.refreshControl = UIRefreshControl()
//
// self.refreshControl?.addTarget(self, action: "refreshList", forControlEvents: .ValueChanged)
loadData()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return shiftData.count
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
let cell:AvailableShiftsCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as AvailableShiftsCell
let shift: PFObject = shiftData.objectAtIndex(indexPath!.row) as PFObject
cell.locationLabel.text = shift.objectForKey("Location") as String?
cell.shiftLabel.text = shift.objectForKey("Shift") as String?
cell.dateLabel.text = shift.objectForKey("Date") as String?
cell.id.text = shift.objectId
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let shift: PFObject = shiftData.objectAtIndex(indexPath.row) as PFObject
shiftID = shift.objectId
println(shiftID)
}
func loadData () {
var getShifts = PFQuery(className: "Shifts")
getShifts.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]!, error:NSError!) -> Void in
if (error == nil) {
self.shiftData.removeAllObjects()
for object in objects {
self.shiftData.addObject(object)
}
let array: NSArray = self.shiftData.reverseObjectEnumerator().allObjects
self.shiftData = array.mutableCopy() as NSMutableArray
self.tableView.reloadData()
}
}
}
@IBAction func refreshButtonPressed(sender: AnyObject) {
loadData()
}
// func refreshList() {
//
// loadData()
//
// self.refreshControl?.endRefreshing()
//
// }
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "details") {
var svc = segue.destinationViewController as ShiftOverview;
svc.shiftID = shiftID
println(shiftID)
}
}
}
答案 0 :(得分:3)
根本不需要为您的应用程序实现didSelectRowAtIndexPath
。如果segue是从单元格构成的,则prepareForSegue:sender:
中的sender参数将是单元格。您可以使用表格查看方法indexPathForCell:
来获取indexPath
,从而获得所需的PFObject
。