I am trying to create a dictionary type app with section header, title text, and subtitle text. I managed to get everything to work using the following code. The only thing I can't figure out is how to filter the UISearchBar to show the results of the String [Coptic] within the array. Is this even possible the way I organized everything or do I need to restructure? Does anyone have any suggestions?
I left the code for the updateSearchResults blank on the bottom because I don't know what to code.
class Gamma: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {
struct words {
var sectionName : String!
var coptic : [String]!
var english : [String]!
}
var array = [words]()
var filtered = [words]()
var filtered2 = UITableViewController()
var indexarray = ["Ga", "Ge", "Gy", "Gn", "Go", "Gr", "Gw"]
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont(name:"CS Avva Shenouda", size:17), NSAttributedStringKey.foregroundColor.rawValue:UIColor.black]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = defaultTextAttribs as Any as! [String : Any]
self.navigationController?.navigationBar.tintColor = UIColor.white
filtered = array
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
self.navigationItem.titleView = searchController.searchBar
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 400
tableView.sectionHeaderHeight = 40
tableView.sectionIndexBackgroundColor = UIColor.clear
tableView.sectionIndexMinimumDisplayRowCount = 15
array = [words(sectionName: "Ga", coptic: ["Gabriyl", "gaza", "gazovulakion", "gala", "galynyc", "Galile`a", "galileoc", "gamoc", "gar"], english: ["ghabriyēl\n\n(Heb.) - Gabriel", "ghå`zå\n\n(f.) - Treasure, money chest", "ghåzo`filåk`yon\n\n(Gk.) - Treasury", "ghå`lå\n\n(Gk. m.) - Milk", "ghålēnēs\n\n(Gk. f.) - Gentle, calm", "ghålilā`å\n\nGalilee", "ghålilā`os\n\n(adj.) - Galilean", "ghåmos / gåmos\n\n(Gk. m.) - Wedding, marriage", "ghår\n\n(Gk. conj.) - For, because"]),
words(sectionName: "Ge", coptic: ["geenna", "gene`a", "genecic", "genneoc", "gennyma", "gennycic", "gennytyc", "gennytria", "genoc", "Gewrgioc"], english: ["ge`ā`nå\n\n(Heb.) - Gehenna, Hades", "gene`å\n\n1. (Gk. f.) - Generation\n\n2. (Gk. f.) - New, recent", "genesis\n\n(Gk. f.) - Birth, start, inception", "gennā`os\n\n(Gk. adj.) - Brave, noble, honorable, good", "gennē`må\n\n(Gk. m.) - Offspring, product", "gennē`sis\n\n(Gk. f.) - Birth, generation", "gennē`tēs\n\n(Gk. m.) - Parent, generator", "gennēt`riyā\n\n(Gk. f.) - Parent, generator", "gā`nos\n\n(Gk. m.) - Race, tribe", "ge`ōrgi`yos\n\nGeorge"]),
words(sectionName: "Gy", coptic: ["gy"], english: ["gē\n\n(Gk. f.) - Earth, ground, land"]),
words(sectionName: "Gn", coptic: ["gnovoc", "gnwmy", "gnwcic"], english: ["ghno`fos\n\n(Gk. m.) - Darkness, gloom", "ghnō`mē\n\n(Gk. f.) - Opinion, thought, judgement", "ghnō`sis\n\n(Gk. f.) - Knowledge"]),
words(sectionName: "Go", coptic: ["Golgo;a", "Gomorra", "gonato"], english: ["gholghothå\n\n(Heb. f.) - Calvary, Golgotha", "ghomorrå\n\n(Heb.) - Gomorrah", "ghonå`to\n\n(Gk.) - Knee"]),
words(sectionName: "Gr", coptic: ["grammateuc", "grammatiky", "gravy"], english: ["gråmmå`tevs\n\n(Gk. m.) - Scribe, secretary", "gråmmå`tikē\n\n(Gk. f.) - Grammar", "ghrå`fē\n\n(Gk. f.) - Writing, drawing, book"]),
words(sectionName: "Gw", coptic: ["Gwg"], english: ["gōg\n\nGog"]),
]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
cell?.textLabel?.text = array[indexPath.section].coptic[indexPath.row]
cell?.detailTextLabel?.text = array[indexPath.section].english[indexPath.row]
cell?.textLabel?.font = UIFont(name:"CS Avva Shenouda", size:30)
cell?.detailTextLabel?.font = UIFont(name: "Constantia", size:25)
cell?.textLabel?.numberOfLines = 0
cell?.detailTextLabel?.numberOfLines = 0
cell?.textLabel?.textColor = UIColor.black
cell?.detailTextLabel?.textColor = UIColor.darkGray
return cell!
}
override func numberOfSections(in tableView: UITableView) -> Int {
return array.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array[section].coptic.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return array[section].sectionName
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return indexarray
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "CS Avva Shenouda", size: 25)!
header.textLabel?.textColor = UIColor.black
header.backgroundView?.backgroundColor = UIColor.lightGray
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row \(indexPath.row) selected")
}
func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text! == "" {
} else {
}
}
}
I try to figure out your problem hope you want the following code to get output