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
}
}
fügen Sie eine Zeile nach Änderungen in Ihrem Nsuedefault dass userDefaults.synchronize() –
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
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 –