Hello!
I am a coding novice learning SWIFT alone for one year, now writing the first app in my life ( a Chinese dictionary) by adopting some source codes available online.
I am thinking about enriching dictionary content. Below is from the swift file (DictionaryContent) where I store my dictionary contents
let WORDS = [
"o": "I",
"oo": "You",
"ooo": "he",
]
"lemma + meaning" seems just too simple. There should be definitely more infos , such as phonetics, POS, example etc. I have thought about perhaps using Struct to store data, but dunno how to put it into practice...
In addition, I think I shall also modify code on ViewController to correspond to the changes in DictionaryContent. However, I really have no clue how to change.
Here is ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
var words: [String: String] = [:]
//maybe this shall be modified?
var searchWords: [String: String] = [:]
//maybe this shall be modified?
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView(frame: .zero)
self.loadWords()
}
func loadWords (){
self.words = WORDS
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchWords.keys.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SuggestionTableViewCell.self), for: indexPath) as! SuggestionTableViewCell
print(cell)
let key = Array(searchWords.keys)[indexPath.row]
cell.wordLbl.text = key
cell.meaningLabel.text = searchWords[key] ?? ""
return cell
}
}
extension ViewController:UISearchBarDelegate{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchWords = words.filter({ (key, value) -> Bool in
return key.contains(searchText)
})
tableView.reloadData()
}
}
Any advice would be wholeheartedly appreciated! Thanks in advance!