Я пытался выяснить, как это решить, и я смог удалить любую строку, но поскольку я не знал, как удалить выбранную строку из своих данных userdefault
, она просто исчезла временно. Я создал экземпляр с именем sections
, который содержит sectionName
и words
. Я хотел бы получить доступ к словам и удалить выбранный по строкам в UITableView
. Также есть ошибка. Ошибка говорит Type [String]! не имеет подписчиков. Кажется, что есть проблема с этой линией;Как удалить данные userdefault в строках в разделе в Swift?
removeData(index: sections[indexPath.section].words[indexPath.row])
И вот этот код в следующем;
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
}
}
добавить одну строку после изменений в вашем Nsuedefault, что userDefaults.synchronize() –
Ошибка говорит Тип [String]! не имеет подписчиков. Кажется, что есть проблема с этой линией; removeData (index: sections [indexPath.section] .words [indexPath.row]) – Ryo
похоже, что вы передаете строку функции, которая принимает int? разделы [indexPath.section] .words [indexPath.row] должны возвращать строку –