2016-10-28 2 views
0

이 문제를 해결하는 방법을 알아 내려고 노력했지만 모든 행을 삭제할 수 있었지만 이후 userdefault 데이터에서 선택한 행을 삭제하는 방법을 알지 못해 일시적으로 사라졌습니다. sections이라는 인스턴스를 만들고 있는데이 인스턴스는 sectionNamewords입니다. 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 
    } 
} 
+0

Nsuedefault에서 변경 한 후 한 줄 추가 userDefaults.synchronize() –

+0

유형 [문자열] 오류가 발생했습니다! 아래 첨자 멤버가 없습니다. 이 줄에는 문제가있는 것 같습니다. removeData (index : sections [indexPath.section] .words [indexPath.row]) – Ryo

+0

은 int를 취하는 함수에 문자열을 전달하는 것처럼 보입니까? 섹션 [indexPath.section] .words [indexPath.row]는 문자열을 반환해야합니다 –

답변

0

나는 알 수 있듯이 데이터에서 주어진 단어를 제거하려고합니다. 다음을 수행하는 것이 좋습니다 :

먼저 단어를 찾으십시오. getData.count > 0 문장 본문. 이 기능으로 removeData 기능을 대체보다

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

:

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") 
} 

는 희망이 도움이!

+0

고맙지 만 오류는 섹션 5의 행 수가 무효라고 말했습니다. 업데이트 (1) 후 기존 섹션에 포함 된 행 수는 업데이트 (1) 이전에 해당 섹션에 포함 된 행 수와 같아야합니다 또는 그 섹션 (삽입 된 0, 삭제 된 1)에서 삽입되거나 삭제 된 행 수를 빼고 해당 섹션으로 또는 그 섹션으로 이동 된 행 수를 더하거나 뺍니다 (0은 이동되고 0은 이동되지 않습니다). – Ryo

+0

tableView의 데이터 소스를 업데이트해야하기 때문에 충돌이 발생합니다. 이 게시물을 어떻게하는지보십시오 : http://stackoverflow.com/questions/8306792/uitableview-reload-section – dirtydanee