2015-01-02 95 views
-2

我一直在關注本教程(https://www.youtube.com/watch?v=Q6qcrO8uNzU&feature=youtu.be)。以下是最後生成的代碼。然而,對於getShifts.findObjectsInBackgroundWithBlock { (objects:AnyObject[]!, error:NSError!) -> Void in使用UITableViewController和單元格解析

我收到一個錯誤,說我應該把AnyObject[]![AnyObject]! ...但這只是創造更多的錯誤。有任何想法嗎?

import UIKit 

class AvailableShifts: UITableViewController { 

var shiftData: NSMutableArray! 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(true) 

    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 2//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.shiftLabel.text = shift.objectForKey("Shift") as String 

    // Configure the cell... 

    return cell 
} 


/* 
// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the specified item to be editable. 
    return true 
} 
*/ 

/* 
// Override to support editing the table view. 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     // Delete the row from the data source 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } else if editingStyle == .Insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    }  
} 
*/ 

/* 
// Override to support rearranging the table view. 
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the item to be re-orderable. 
    return true 
} 
*/ 

/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

func loadData() { 

    shiftData.removeAllObjects() 

    var getShifts = PFQuery(className: "Shifts") 

    getShifts.findObjectsInBackgroundWithBlock { 
     (objects:AnyObject[]!, error:NSError!) -> Void in 

     if (error == nil) { 

      for object: PFObject! in objects { 

       self.shiftData.addObject(object) 
      } 

      let array: NSArray = self.shiftData.reverseObjectEnumerator().allObjects 

      self.shiftData = array as NSMutableArray 

      self.tableView.reloadData() 
     } 

    } 

} 

} 
+0

不確定問題到底是什麼,但是如果xcode根據您的實現產生了一個建議,則可能最符合您的最佳利益,請留意該建議並排除其餘錯誤。在插入AnyObject的正確語法後,究竟是什麼錯誤?查看[HERE](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html)瞭解[AnyObject]實際上在做什麼。搜索NSArray – soulshined

回答

0

嘗試,幫助下面,在頂部var shiftData: NSMutableArray!var shiftData = [PFObject]()

先改變你的FUNC loadData()

func loadData() { 

    var getShifts = PFQuery(className: "Shifts") 

    getShifts.findObjectsInBackgroundWithBlock { 
    (objects: [AnyObject]!, error: NSError!) -> Void in 
    if error == nil { 

    var shiftData = objects as [PFObject] 
    self.shiftData = shiftData 
    var lastIndex = NSIndexPath(forRow: self.shiftData.count - 1, inSection: 0) 

    } 
} 
} 

,並在的cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCellWithIdentifier("Cell" as AvailableShiftsCell 
    let shift = self.shiftData[indexPath.row] 
     cell.shiftLabel.text = shift as String 

    return cell 
} 

希望。

相關問題