简体   繁体   English

reloadSections UITableView Swift

[英]reloadSections UITableView Swift

I am trying to reload section and update the number of cells in a section, but I'm getting an error and crash whenever I try. 我试图重新加载部分并更新一个部分中的单元格数量,但每当我尝试时,我都会收到错误并崩溃。 Here is the code I am using: 这是我正在使用的代码:

import UIKit
import CalendarView
import SwiftMoment

class TimeAwayRequestTableViewController: UITableViewController {

@IBOutlet var calendarView: CalendarView!

var selectedDates : [Moment] = []

override func viewDidLoad() {
    super.viewDidLoad()

    calendarView.delegate = self
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var returnInt = 0

    if section == 0 {
        returnInt = 2
    }
    if section == 1 {
        returnInt = 1
    }

    if section == 2 {
        print(selectedDates.count)
       returnInt = selectedDates.count
    }

    return returnInt
}

}

extension TimeAwayRequestTableViewController : CalendarViewDelegate {

func calendarDidSelectDate(date: Moment) {
    selectedDates.append(date)
    print(selectedDates)
    let section = NSIndexSet(index: 2)
    self.tableView.beginUpdates()
    self.tableView.reloadSections(section, withRowAnimation: .Automatic)
    self.tableView.endUpdates()
}

func calendarDidPageToDate(date: Moment) {
    print(date)
}

}

So basically when the date is tapped in the calendar, I add it to the Array and then update the number of cells. 因此,基本上在日历中点击日期时,我将其添加到数组中,然后更新单元格数。 I haven't gotten to the point where I configure the cell content, right now I'm just trying to make sure this is working like i want. 我还没有达到配置单元格内容的程度,现在我只是想确保它的工作方式与我想要的一样。 The error I get is: 我得到的错误是:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

But i don't understand why because it counts and shows 2. So I'm confused. 但我不明白为什么因为它很重要并且显示2.所以我很困惑。

我敢打赌你在func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell使用了一些数组(包括selectedDates ),并且超出范围,请检查数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM