swift:fatal error: Array index out of range (lldb)


import UIKit
import Alamofire



class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView:UITableView?
    
    var shujuyuan: Array<String> = []
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        

        Alamofire.request(.GET, "http://chifanbao.com/index.php/Home/Table/displayTable", parameters: ["robot": "123","shopid":"22"])
            .responseJSON { response in
                
                if let JSON = response.result.value {
                  //  print("JSON: \(JSON)")
                    
              //      let XXX = JSON as! NSMutableArray
                    
                 
                    
                  //  print(JSON[0]["table_id"])
                    
                   for item in JSON as! NSMutableArray
                   {
                    
                  //  print(item["table_id"])
                    var a = item["table_name"] as! String
                   self.shujuyuan.append(a)
                    
                    }
                    
                    print(self.shujuyuan)
                  //  print(self.shujuyuan[0])
                    
                    
                    
                }
        }
        
        
        //创建表视图
        self.tableView = UITableView()
        self.tableView!.frame = CGRectMake(0, 0, self.view.frame.width,
            self.view.frame.height)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self,
            forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(self.tableView!)
        
        //默认文字颜色是黑色,选中项文字是白色

    }
    
    //在本例中,只有一个分区
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
    
    //返回表格行数(也就是返回控件数)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    //创建各单元显示内容(创建参数indexPath指定的单元)
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell
    {
        //为了提供表格显示性能,已创建完成的单元需重复使用
        let identify:String = "SwiftCell"
        //同一形式的单元格重复使用,在声明时已注册
        let cell = tableView.dequeueReusableCellWithIdentifier(identify,
            forIndexPath: indexPath) as UITableViewCell
       
        cell.textLabel?.text = self.shujuyuan[indexPath.row]
        
        print(self.shujuyuan.dynamicType)
        

        
        
        
        cell.textLabel?.textColor = UIColor.blackColor()
        cell.textLabel?.highlightedTextColor = UIColor.whiteColor()
        
        //选中背景修改成绿色
        cell.selectedBackgroundView = UIView()
        cell.selectedBackgroundView?.backgroundColor =
            UIColor(red: 135/255, green: 191/255, blue: 49/255, alpha: 1)
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

clipboard.png

阅读 3.8k
1 个回答

打个断点,查看 shujuyuan 这个数组的值是多少。然后你就知道该怎么解决了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进