“Attempt to delete row from section 1, but there are only 1 sections before update”

I'm trying to delete the rows that don't meet the criteria in the for loop. However, I'm getting and error that says: 'attempt to delete row 0 from section 1, but there are only 1 sections before the update." I'v never seen this before and not sure why I am getting it.

My code:

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

    let cell = theTableView.dequeueReusableCellWithIdentifier("MyCell") as! TableViewCell
    cell.titleLabel.text = titles[indexPath.row]
    cell.priceLabel.text = prices[indexPath.row]
    cell.descLabel.text = descs[indexPath.row]
    cell.itemImage.image = itemImages[indexPath.row]
    cell.userNumber = phoneNumbers[indexPath.row] as! String
    cell.timeLabel.text = datesHours[indexPath.row]! + "hr"
    cell.distanceLabel.text = String(locations[indexPath.row]!) + "mi"
    cell.viewController = self


    for (index, number) in self.locations {
        if number <= 5 {
            let indexPath = NSIndexPath(forRow: number, inSection: 1)
            self.theTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)



It seems like you are telling the table there will be more rows than you actually want to display and then deleting them after the fact.

Instead you should be checking if the elements in the array meet the criteria in (or before) numberOfRowsInSection and putting them into a different array that will actually be displayed so that the table knows how many rows will actually be displayed. Then in cellForRowAtIndexPath just use the newly create array that has the data that will actually be displayed.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.

        self.displayArray = []
        for (index, number) in self.locations {
            if number <= 5 {
        return self.displayArray.Count

I'm assuming your error has something to do with you trying to update the table in a method that is first trying to create it. You're trying to update something that hasn't fully been created.

Need Your Help

jQuery show/hide not working

javascript jquery html5 css3

I'm having the strangest problem with jQuery's hide() and show() functions.

Contextual Domain Driven Model Validation

c# validation domain-driven-design domain-model application-layer

In our application we have a scenario where we need to validate an property update based on business rules and the context of the current user. I am trying to determine the best way to do the vali...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.