我有这个代码用Swift从TableView中加载 Parse 的数据。 我遇到的问题:
NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引2 超出空数组'
的界限
在触摸之前,我怎么能强制等待视图加载所有单元数据? 我可以将loadData()调用放在viewDidLoad方法中,但是这样我每次添加新单元格时都会强制刷新数据,删除单元格......等等。
class MasterTableViewController: UITableViewController {
var alertsListData:NSMutableArray = NSMutableArray()
func loadData(){
alertsListData.removeAllObjects()
var currentUser = PFUser.currentUser()
if (currentUser != nil){
var findAlertsData = PFQuery(className: "Alert")
findAlertsData.whereKey("user_id", equalTo:PFUser.currentUser())
findAlertsData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!)-> Void in
if (error == nil){
for object in objects {
self.alertsListData.addObject(object)
}
self.tableView.reloadData()
self.refreshControl?.endRefreshing()
}
}
}
}
@IBAction func logoutAction(sender: AnyObject) {
PFUser.logOut()
var currentUser = PFUser.currentUser()
}
override func viewDidAppear(animated: Bool) {
self.loadData()
...
}
override func viewDidLoad() {
super.viewDidLoad()
var refresh = UIRefreshControl()
refresh.attributedTitle = NSAttributedString(string: "Tirer pour rafraîchir")
refresh.addTarget(self, action: "loadData", forControlEvents:.ValueChanged)
self.refreshControl = refresh
// 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 alertsListData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:AlertTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as AlertTableViewCell
let alert:PFObject = self.alertsListData.objectAtIndex(indexPath.row) as PFObject
cell.alertTitleLabel.text = alert.objectForKey("title") as? String
var dateUpdated = alert.updatedAt as NSDate
var dateFormat = NSDateFormatter()
dateFormat.locale = NSLocale(localeIdentifier: "fr_FR")
dateFormat.dateFormat = "EEEE d, à hh:mm"
cell.alertDateLabel.text = NSString(format: "%@", dateFormat.stringFromDate(dateUpdated).capitalizedString)
var status:Bool = alert.objectForKey("status") as Bool
if (status == true){
cell.alertStatus.text = "Terminée"
}else{
cell.alertStatus.text = "En cours"
}
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.
if (segue!.identifier == "showDetails"){
var selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
var detailViewController:DetailViewController = segue!.destinationViewController as DetailViewController
detailViewController.alertData = alertsListData.objectAtIndex(selectedIndexPath.row) as PFObject
}
}
}
添加控制器是:
class AddViewController: UIViewController {
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var searchTextField: UITextField!
@IBOutlet weak var urlTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func addButtonTapped(sender: AnyObject) {
var toAdd = PFObject(className:"Alert")
toAdd["title"] = titleTextField.text
toAdd["user_id"] = PFUser.currentUser()
toAdd["status"] = false
toAdd.saveInBackground()
self.navigationController?.popToRootViewControllerAnimated(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.
}
*/
}
答案 0 :(得分:0)
也许您可以切换用户交互,直到收到并重新加载数据?
self.tableView.userInteractionEnabled = false
同时将self.loadData()
移至viewDidLoad
,每次返回时都无需重新下载