2016-07-09 6 views
2

ich versuche, eine Zelle aus einer TableViewController zu entfernen. Jedes Mal, wenn ich wische und die Löschtaste drücke, kommt es zu einem Absturz.Wenn ich Zelle aus TableView auf Schaltfläche Aktion entfernen, ist die App abgestürzt - iOS Swift

Dies ist das Crash-Protokoll:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (10) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

und das ist meine UITableView Funktionen.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let myCell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! PredefinedServicesCell 

    //Cell configuration. 
    myCell.selectionStyle = .None 
    myCell.containerView.layer.borderColor = UIColor(hex: 0x3399CC).CGColor 
    myCell.containerView.layer.borderWidth = 1 
    myCell.containerView.layer.cornerRadius = 10 
    myCell.containerView.clipsToBounds = true 
    myCell.servicePrice.text = "\(indexPath.row)" 
    myCell.serviceCurrency.text = "KWD" 
    myCell.serviceTitle.text = "Building iOS Application" 
    myCell.serviceDescription.text = "This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description" 
    return myCell 
} 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return true 
} 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

    if editingStyle == .Delete { 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } 

} 

was könnte der Grund sein?

Antwort

0

Sie können keine statischen Wert zu Ihrem numberOfRowsInSection deklarieren und dann ändern (10-1) durch eine Reihe mit deleteRowsAtIndexPaths entfernen, bleiben numberOfRowsInSection 10 und deine Zeilen zu 9, so dass Sie diese Art von Problem haben, machen diese Änderungen es ist

var totalRows: Int = 10 

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

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

    if editingStyle == .Delete { 
     if self.totalRows > 0 { // This limit your deletion to avoid a crash 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
      self.totalRows -= 1 
     } 
    } 
} 

Regel besser eine Datenquelle Array zu behandeln Ihre Informationen zu erhalten in jeder Zelle zu zeigen, so denkt einen Array verwenden, um es so können Sie es tun verwenden: einen dynamischen Wert habencount Eigenschaft und entfernen Sie das Element, wenn Sie eine Zelle löschen.

0

Sobald Sie die Zeilen aus der Tabellenansicht entfernt haben, müssen Sie auch Ihren numberOfRowsInSection-Wert aktualisieren. Deshalb stürzt man ab.

let rows=10 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return rows 
    } 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let myCell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! PredefinedServicesCell 

     //Cell configuration. 
     myCell.selectionStyle = .None 
     myCell.containerView.layer.borderColor = UIColor(hex: 0x3399CC).CGColor 
     myCell.containerView.layer.borderWidth = 1 
     myCell.containerView.layer.cornerRadius = 10 
     myCell.containerView.clipsToBounds = true 
     myCell.servicePrice.text = "\(indexPath.row)" 
     myCell.serviceCurrency.text = "KWD" 
     myCell.serviceTitle.text = "Building iOS Application" 
     myCell.serviceDescription.text = "This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description" 
     return myCell 
    } 
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     return true 
    } 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

     if editingStyle == .Delete { 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
      rows-=1 
     } 

    } 
0

das Crash-Protokoll folgend, sollten Sie Anzahl der Zeilen aktualisieren, nachdem die Bearbeitung

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
} 
0

Sie nicht die Anzahl der Zelle als 10 zurückkehren können, nachdem die Zelle zu löschen. Es sollte weniger als die Anzahl der Zellen vor dem Löschen sein.

3

Bitte fügen Sie Ihre numberOfRowsInSection dynamisch hinzu. Es ist nicht statisch.

Verwenden Sie das Array anstelle von return 10. und entfernen Sie objec, wenn Sie im Bearbeitungsstil löschen.

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

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

    if editingStyle == .Delete { 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

// remove object from array here 

    } 

} 

Grund für Fehler: in Ihrem Fall

Tannen Sie nicht die Tabellenzellenzahl ist 10. Wenn Sie löschen Zelle Ihrer Zelle gelöscht wird aber Zahl ist 10. Es ist worng sonst verringert Ihre zählen zu 9.

0

Sie müssen Ihre Datenquelle aktualisieren, um die Anzahl der Zeile/Abschnitt Ihrer Tabellenansicht zu entsprechen, und verwenden Sie beginUpdate & EndUpdate, um Ihre Tabellenansicht zu aktualisieren. Wie so:

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

    if editingStyle == .Delete { 
     tableView.beginUpdate() //optional 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     //TODO ... add your code to update data source here 

     tableView.endUpdate() //optional 
    } 

} 
+1

Es gibt ein häufiges Missverständnis: 'start/endUpdates' wird für eine einzelne Operation überhaupt nicht benötigt. Es ist nur für mehrere Operationen zum Einfügen/Löschen/Verschieben erforderlich, die sich gegenseitig stören könnten. – vadian

+0

Das Anfangs-/Ende-Update wird nicht nur zum Einfügen/Löschen mehrerer Zeilen/Abschnitte verwendet, sondern es aktualisiert auch die Höhe Ihrer Zeilen, die auf dem Bildschirm angezeigt werden. Ansonsten stimme ich zu, dass es für die Operation für eine Zeile nicht notwendig ist. –

1

eine Aufzeichnung der Anzahl der gelöschten Zellen halten und die gleiche Menge von Array löschen (verwendet Ansicht aufzufüllen Tabelle) als auch.

0

Ihre numberOfRowsInSection sollte dynamisch nicht statisch sein.So entfernen Sie Daten im dynamischen Array mit yourdataName.remove(at:indexPath.row)

Beispiel;

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return yourdataName.count; 
    } 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
       if editingStyle == UITableViewCellEditingStyle.delete { 
        yourdataName.remove(at:indexPath.row) 
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
       } 
      } 
Verwandte Themen