Swift index 0 beyond bounds for empty array in tableview

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I'm trying to populate tableview with parse with 2 labels connected to the the main tv controller with PFTableViewCell

when I add the (numberOfSectionsInTableView + numberOfRowsInSection ) the app crash

but when I deleted it it works but it show nothing.

This the table view cell

class courseCell: PFTableViewCell {      @IBOutlet var name: UILabel!      @IBOutlet var location: UILabel!  } 

This the table view controller

class courseTVC: PFQueryTableViewController {   override init!(style: UITableViewStyle, className: String!) {     super.init(style: style, className: className) }   required init(coder aDecoder:NSCoder) {     super.init(coder:aDecoder)        self.parseClassName = "courses"     self.textKey = "Location"     self.pullToRefreshEnabled = true     self.paginationEnabled = false  } override func queryForTable() -> PFQuery! {     var query = PFQuery(className: "courses")     query.orderByDescending("Location")     return query }     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {         return 1     }  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     return 4 }   override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell {     var cell = tableView.dequeueReusableCellWithIdentifier("cell" , forIndexPath : indexPath) as? courseCell     if cell == nil     {         cell = courseCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")     }       cell!.name.text = object["Price"] as! String!     cell!.location.text = object["Location"] as! String!         return cell! } 

I don't know how to fix this issue

回答1:

Looking at the documentation for PFQueryTableViewController, https://www.parse.com/docs/ios/api/Classes/PFQueryTableViewController.html it looks like you shouldn't be overriding those two methods.

should be called already for all the rows in your table based on your objects. You shouldn't override numberOfSectionsInTableView and numberOfRowsInSection because the Parse controller handles all of that for you. You are overriding these methods and hardcoding a number which causes Parse to try to fetch and object for a row that is out of bounds (it doesn't exist). It looks like there are actually no objects in your datasource.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!