我一直试图弄清楚如何解决这个问题并且我能够删除任何行但由于我不知道如何从其userdefault
数据中删除所选行,因此它暂时消失了。我创建了一个名为sections
的实例,其中包含sectionName
和words
。我想在UITableView
中的行中访问单词并删除选定的单词。还有一个错误。错误显示Type [String]!没有下标成员。这条线似乎有问题;
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
}
}
答案 0 :(得分:0)
正如我所看到的,您正在尝试从数据中删除给定的单词。我建议做以下事情:
首先找到您要删除的字词,.data
语句正文。
getData.count > 0
用这个函数替换if getData().count > 0 {
let section = sections[indexPath.section
let word = section.words[indexPath.row]
remove(word)
}
函数:
removeData
希望它有所帮助!