이 문제를 해결하는 방법을 알아 내려고 노력했지만 모든 행을 삭제할 수 있었지만 이후 userdefault
데이터에서 선택한 행을 삭제하는 방법을 알지 못해 일시적으로 사라졌습니다. sections
이라는 인스턴스를 만들고 있는데이 인스턴스는 sectionName
과 words
입니다. UITableView
에서 단어에 액세스하고 행에서 선택한 단어를 삭제하고 싶습니다. 또한 오류가 있습니다. 오류 [유형] [유형]라고! 아래 첨자 멤버가 없습니다. 이 줄에는 문제가있는 것 같습니다.Swift에서 섹션의 행에있는 userdefault 데이터를 삭제하는 방법은 무엇입니까?
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() –
유형 [문자열] 오류가 발생했습니다! 아래 첨자 멤버가 없습니다. 이 줄에는 문제가있는 것 같습니다. removeData (index : sections [indexPath.section] .words [indexPath.row]) – Ryo
은 int를 취하는 함수에 문자열을 전달하는 것처럼 보입니까? 섹션 [indexPath.section] .words [indexPath.row]는 문자열을 반환해야합니다 –