2016-10-28 1 views
0

Ich habe versucht, herauszufinden, wie das zu lösen ist und ich konnte jede Zeile löschen, aber da ich nicht wusste, wie Sie die ausgewählte Zeile aus seinen userdefault Daten löschen, ist es nur vorübergehend verschwunden. Ich habe eine Instanz namens sections erstellt, die sectionName und words halten. Ich möchte auf Wörter zugreifen und eine ausgewählte in Zeilen in UITableView löschen. Auch hier ist ein Fehler aufgetreten. Der Fehler sagt Typ [String]! hat keine tiefgestellten Mitglieder. Es scheint, dass es ein Problem mit dieser Linie gibt;Wie löscht man Benutzerdefault-Daten in Zeilen in einem Abschnitt in Swift?

removeData(index: sections[indexPath.section].words[indexPath.row]) 

Und das ist der Code im Folgenden;

struct Section { 
    var sectionName: String! 
    var words: [String]! 

    init(title: String, word: [String]) { 
     self.sectionName = title 
     self.words = word 

    } 
} 
var sections = [Section]() 
     sections = [ 
     Section(title: "A", word: []), // 1 
     Section(title: "B", word: []), //2 
     Section(title: "C", word: []), 
     Section(title: "D", word: []), 
     Section(title: "E", word: []), 
     Section(title: "F", word: []), 
     Section(title: "G", word: []), 
     Section(title: "H", word: []), 
     Section(title: "I", word: []), 
     Section(title: "J", word: []), 
     Section(title: "K", word: []), 
     Section(title: "L", word: []), 
     Section(title: "M", word: []), 
     Section(title: "N", word: []), 
     Section(title: "O", word: []), 
     Section(title: "P", word: []), 
     Section(title: "Q", word: []), 
     Section(title: "R", word: []), 
     Section(title: "S", word: []), 
     Section(title: "T", word: []), 
     Section(title: "U", word: []), 
     Section(title: "V", word: []), 
     Section(title: "W", word: []), 
     Section(title: "X", word: []), 
     Section(title: "Y", word: []), 
     Section(title: "Z", word: []) 
    ] 




func getData() -> [String] { 
    if let data = userdefaultData.stringArray(forKey: "data") { 

     return data 
    } 
    return [] 
} 


func removeData(index: Int) { 
    var data = getData() 
    data.remove(at: index) 
    userdefaultData.set(data, forKey: "data") 
} 

// Override to support editing the table view. 
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 

    if editingStyle == .delete { 
     // Delete the row from the data source 

     tableView.beginUpdates() 

     // Delete the row from the data source 
     if getData().count > 0 { 
      removeData(index: sections[indexPath.section].words[indexPath.row]) 
     } 

     tableView.deleteRows(at: [indexPath], with: .fade) 

     tableView.endUpdates() 



    } else if editingStyle == .insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
+0

fügen Sie eine Zeile nach Änderungen in Ihrem Nsuedefault dass userDefaults.synchronize() –

+0

Fehler sagt Typ [String]! hat keine tiefgestellten Mitglieder. Es scheint, dass es ein Problem mit dieser Linie gibt; removeData (index: sections [indexPath.section] .words [indexPath.row]) – Ryo

+0

sieht so aus, als würden Sie einen String an eine Funktion übergeben, die einen int-Wert annimmt. sections [indexPath.section] .words [indexPath.row] sollte eine Zeichenkette zurückgeben –

Antwort

0

Wie ich sehen kann, versuchen Sie ein bestimmtes Wort aus Ihren Daten zu entfernen. Ich würde empfehlen, Folgendes zu tun:

zuerst finden Sie das Wort, das Sie entfernen möchten, getData.count > 0 Anweisungen Körper.

if getData().count > 0 { 
     let section = sections[indexPath.section 
     let word = section.words[indexPath.row] 
     remove(word) 
} 

als die removeData Funktion mit dieser Funktion ersetzen:

func remove(_ word: String) { 
    var data = getData() 

    // Lets search for the removeable word's index in the array 
    var index = 0 
    for wordCandidate in data { 
     if wordCandidate == word { 
      // Once found, exit from the loop 
      return 
     } 
     // if not found, increase the index by one 
     index += 1 
    } 
    // Remove the word at the right index 
    data.remove(at: index) 
    userdefaultData.set(data, forKey: "data") 
} 

Hoffe, es hilft!

+0

Danke, aber ein Fehler hat eine ungültige Anzahl von Zeilen in Abschnitt 5 angegeben. Die Anzahl der Zeilen in einem vorhandenen Abschnitt nach dem Update (1) muss der Anzahl der Zeilen in diesem Abschnitt vor dem Update (1) plus entsprechen oder minus der Anzahl der Zeilen, die aus diesem Bereich eingefügt oder gelöscht wurden (0 eingefügt, 1 gelöscht) und plus oder minus der Anzahl der Zeilen, die in diesen Bereich hinein- oder herausgezogen wurden (0 eingezogen, 0 ausgezogen). ' – Ryo

+0

es stürzt ab, weil Sie auch die Datenquelle des TableView aktualisieren müssen. Schau dir diesen Post an, wie es geht: http://stackoverflow.com/questions/8306792/uitableview-reload-section – dirtydanee

Verwandte Themen