Arrays 数组索引超出范围tableview具有“加载更多”按钮

Arrays 数组索引超出范围tableview具有“加载更多”按钮,arrays,swift,uitableview,Arrays,Swift,Uitableview,我有一个tableview,它从NSDate数组获取数据。我返回数组计数+1作为tableview中的行数,因为我希望tableview底部有一个按钮,允许用户加载更多单元格。以下是我的部分代码: var dates = [NSDate]() func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.

我有一个tableview,它从NSDate数组获取数据。我返回数组计数+1作为tableview中的行数,因为我希望tableview底部有一个按钮,允许用户加载更多单元格。以下是我的部分代码:

var dates = [NSDate]()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == dates.count + 1 {

        let cell = myTableView.dequeueReusableCellWithIdentifier("loadMoreCell", forIndexPath: indexPath) as! LoadMoreTableViewCell

        cell.loadmoreButton.targetForAction("loadmore", withSender: self)
        cell.loadmoreButton.addTarget(self, action: "loadmore:", forControlEvents: .TouchUpInside)

        return cell

    } else {

        let cell = myTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TestingViewCell


        //cell.date.text = dateFormatter.stringFromDate(dates[indexPath.row])

        return cell
    }
}

但是我得到了一个数组索引超出范围的错误,对于注释掉的行,我应该怎么做?提前谢谢

如果indexath.row==dates.count
?假设日期包含2个元素。您希望显示2个元素+更多单元格。这就是3,但您的indexPath将是(行)、0(日期1)、1(日期2)和2(morecell),其中dates.count将是2。@larme谢谢!解决了我的问题