致命错误:数组索引超出范围,地图应用

时间:2016-01-02 03:14:29

标签: ios swift dictionary

我是新手,可以真正使用你的帮助。我正在尝试在xcode中创建一个地图应用程序,swift,但之前有一个表。当我在模拟器中启动我的应用程序时,表格视图很酷。当我点击一个字段(它应该带我到一个带有地图的新视图控制器)时它会崩溃并说[致命错误:数组索引超出范围]突出显示此代码

let sportskiObjekat = sportskiObjektiArray[izabranObjekat]

此表视图控制器中的整个代码如下:

import UIKit

class SportskiObjektiTableViewController: UITableViewController {

   var sportskiObjektiArray = Array<SportskiObjekti>()
   var izabranObjekat:Int = -1


   override func viewDidLoad() {
       super.viewDidLoad()

       // 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()
       let path = NSBundle.mainBundle().pathForResource("SportskiObjekti", ofType: "plist")!
       let array = NSArray(contentsOfFile: path) as! Array<Dictionary<String,AnyObject>>

       for dict in array {
           let sportskiObjekti = SportskiObjekti(dict: dict)
           sportskiObjektiArray.append(sportskiObjekti)
       }

   }

   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 Incomplete implementation, return the number of sections
        return 1
   }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.sportskiObjektiArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...
        let objekat = self.sportskiObjektiArray[indexPath.row]
        cell.textLabel?.text = objekat.name
        cell.detailTextLabel?.text=objekat.location

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

    {
        //naknadno//
        izabranObjekat = indexPath.row
      self.performSegueWithIdentifier("mapViewControllerSegueIdentifier", sender: self)
    }

//novo
   // 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?) {
         let sportskiObjekat = sportskiObjektiArray[izabranObjekat]
         let mapVC = segue.destinationViewController as! MapViewController
         mapVC.objekat = sportskiObjekat

    }
 }

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您忘记重新加载UI以便使用数据填充单元格。所以在追加数组后,添加reloadData()

   self.tableView.reloadData()

你的问题就在这里,你的indexPath.row

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let indexPath = self.tableView.indexPathForSelectedRow()!{  //<-- indexpath
     let sportskiObjekat = sportskiObjektiArray[indexPath.row]
     let mapVC = segue.destinationViewController as! MapViewController
     mapVC.objekat = sportskiObjekat
   }
}