本文介绍了Swift 索引 0 超出 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

推荐答案

查看 PFQueryTableViewController 的文档,https://www.parse.com/docs/ios/api/Classes/PFQueryTableViewController.html 看起来你不应该覆盖这两个方法.

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: 应该已经根据 objects 为表中的所有行调用.您不应该覆盖 numberOfSectionsInTableViewnumberOfRowsInSection 因为 Parse 控制器会为您处理所有这些.您正在覆盖这些方法并对一个数字进行硬编码,这会导致 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 中空数组的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:33