Swift索引0超出TableView中空数组的范围 [英] Swift index 0 beyond bounds for empty array in tableview
问题描述
我正在尝试使用带有PFTableViewCell的2个标签连接到主电视控制器的解析来填充tableview.
I'm trying to populate tableview with parse with 2 labels connected to the the main tv controller with PFTableViewCell
当我添加(numberOfSectionsInTableView + numberOfRowsInSection)应用程序崩溃时
when I add the (numberOfSectionsInTableView + numberOfRowsInSection ) the app crash
但是当我删除它时,它可以工作,但是什么也没显示.
but when I deleted it it works but it show nothing.
这是表格视图单元格
class courseCell: PFTableViewCell {
@IBOutlet var name: UILabel!
@IBOutlet var location: UILabel!
}
这是表视图控制器
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
推荐答案
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.
– tableView:cellForRowAtIndexPath:object:
.您不应覆盖numberOfSectionsInTableView
和numberOfRowsInSection
,因为解析控制器会为您处理所有这些操作.您将覆盖这些方法,并对一个数字进行硬编码,这将导致Parse尝试为超出范围(不存在)的行获取和对象.看起来您的数据源中实际上没有对象.
– tableView:cellForRowAtIndexPath:object:
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.
这篇关于Swift索引0超出TableView中空数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!