__NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0] swift

时间:2015-01-13 11:29:48

标签: uitableview swift core-data sections

我有错误__NSArrayM objectAtIndex:]:当我尝试添加具有相同日期的第二项时,索引1超出了界限[0 .. 0]。

我无法找到问题所在。我认为问题出在getSectionItems函数中。

有人可以帮帮我吗?

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var sumLabel: UILabel!

var costItems: NSMutableArray = NSMutableArray()
var accountIdentifier: NSString = NSString()
var costsSum:Int!

var sectionsInTable = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    costsSum = getCostsSum()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
    loadData()
    self.tableView.reloadData()
    costsSum = getCostsSum()
}


func loadData() {
    costItems.removeAllObjects()

    let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
    let predicate: NSPredicate = NSPredicate(format: "identifier == '\(accountIdentifier)'")!
    let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: predicate, managedObjectContext: moc)

    for cost in results{
        let singleCost:Cost = cost as Cost

        let costDict:NSDictionary = ["identifier":singleCost.identifier,"costName":singleCost.costName, "costValue":singleCost.costValue, "date":singleCost.date]

        costItems.addObject(costDict)

        // Date

        let dateFormatter: NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd MMMM"
        let dateString: NSString = dateFormatter.stringFromDate(costDict.objectForKey("date") as NSDate)

        let sections: NSSet = NSSet(array: sectionsInTable)
        if !sections.containsObject(dateString) {
            sectionsInTable.append(dateString)
        }
    }

    self.tableView.reloadData()
}

func getSectionItems(section: Int) -> [Cost] {
    var sectionItems = [Cost]()
    let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
    let predicate: NSPredicate = NSPredicate(format: "identifier == '\(accountIdentifier)'")!
    let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: predicate, managedObjectContext: moc)

    for cost in results {

        let singleCost:Cost = cost as Cost

        let costDict:NSDictionary = ["identifier":singleCost.identifier,"costName":singleCost.costName, "costValue":singleCost.costValue, "date":singleCost.date]

        let dateFormatter: NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd MMMM"
        let dateString: NSString = dateFormatter.stringFromDate(costDict.objectForKey("date") as NSDate)


        if dateString == sectionsInTable[section] as NSString {
            sectionItems.append(singleCost)
        }
    }

    return sectionItems
}

// UITableViewDataSource

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionsInTable[section]
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sectionsInTable.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.getSectionItems(section).count
}

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

    let cell:DetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DetailTableViewCell

    let costDict:NSDictionary = costItems.objectAtIndex(indexPath.row) as NSDictionary

    let costName = costDict.objectForKey("costName") as String
    let costValue = costDict.objectForKey("costValue") as Double

    cell.nazwaWydatkuLabel.text = costName

    // Date
    let dateFormatter: NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMMM"
    let dateString: NSString = dateFormatter.stringFromDate(costDict.objectForKey("date") as NSDate)
    cell.dateLabel.text = dateString

    if (Int(costValue)) < 0 {
        cell.wartośćLabel.layer.cornerRadius = 8.0
        cell.wartośćLabel.layer.masksToBounds = true
        cell.wartośćLabel.backgroundColor = UIColor(red: 255.0/255.0, green: 59.0/255, blue: 48.0/255.0, alpha: 1.0)
    } else {
        cell.wartośćLabel.layer.cornerRadius = 8.0
        cell.wartośćLabel.layer.masksToBounds = true
        cell.wartośćLabel.backgroundColor = UIColor(red: 127.0/255.0, green: 207.0/255.0, blue: 77.0/255.0, alpha: 1.0)

    }

    cell.wartośćLabel.text = "  \(Int(costValue)) zł  "

    return cell
}

@IBAction func addCostButtonPressed(sender: UIBarButtonItem) {

   self.performSegueWithIdentifier("addCostVC", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "addCostVC" {
        let addCostVC: AddTableViewController = segue.destinationViewController as AddTableViewController
        let thisID = accountIdentifier
        addCostVC.accountIdentifier = thisID
    }
}

func getCostsSum() -> Int {

    let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
    let predicate: NSPredicate = NSPredicate(format: "identifier == '\(accountIdentifier)'")!
    let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: predicate, managedObjectContext: moc)

    var costSum: Int = 0
    for res in results {
        var costCount = res.valueForKey("costValue") as Int
        costSum += costCount
    }
    self.sumLabel.text = "\(costSum) zł"
    return costSum
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
    let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Cost), withPredicate: nil, managedObjectContext: moc)


    let thisCost: AnyObject = results.objectAtIndex(indexPath.row)
    moc.deleteObject(thisCost as NSManagedObject)
    //costsValues = getAccountCountSum()

    SwiftCoreDataHelper.saveManagedObjectContext(moc)

    loadData()
    getCostsSum()
    self.tableView.reloadData()
}

0 个答案:

没有答案