Reputation: 37
I am trying to get the first letter of the first element in a two dimensional array to add as a header for my tableview. The code is below:
var cityArray = [["NYC, NY", New Hampshire"],["Buffalo, NY]]
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.backgroundColor = UIColor.flatOrangeColorDark()
label.textColor = .white
if searching {
} else {
let headerArray = cityArray.compactMap{$0.first?.prefix(1)}
print("\(headerArray)")
for i in headerArray {
label.text = String(i)
//This returns for all of the headers the last element in headerArray
}
}
return label
}
This returns "B" for all headers
Additionally, I would like to make the headers disappear when scrolling. This was achieved by selecting the tableview as grouped. However, this makes each cell the same height. I would like to make the header smaller in height than the other cells. Here is the code currently:
var tableHeaderHeight = 45
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if searching {
return 0
} else {
return CGFloat(tableHeaderHeight)
}
}
override func viewDidLoad() {
super.viewDidLoad()
cityTableview.rowHeight = 75
//need to create a dynamic height for the section
//alternatively using .automaticdimension = true
}
Upvotes: 0
Views: 155
Reputation: 171
The line
var cityArray = [["NYC, NY", New Hampshire"],["Buffalo, NY]]
will not compile due to the missing quotes. I am not sure how your array should exactly look like, but I assume it can be
var cityArray = [["NYC, NY", "New Hampshire"],["Buffalo", "NY"]]
Then you can use a map function to get the first element using first and the first letter using prefix:
let filteredArray = cityArray.map({$0.first?.prefix(1)})
for firstLetter in filteredArray {
if let uFirstLetter = firstLetter {
print(uFirstLetter) // prints N B
}
}
Update: this is a minimum working example which will give you N in the first header and B in the second header:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var cityArray = [["NYC, NY", "New Hampshire"],["Buffalo", "NY"]]
var filteredArray: [String.SubSequence?] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 //just for simplification purpose, change it according to your needs
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//just for simplification purpose, change it according to your needs
let cell = UITableViewCell()
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
tableView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
filteredArray = cityArray.map({$0.first?.prefix(1)})
}
func numberOfSections(in tableView: UITableView) -> Int {
return cityArray.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.backgroundColor = .lightGray
label.textColor = .red
if let uFirstLetter = filteredArray[section] {
label.text = uFirstLetter.description
return label
} else {
return nil
}
}
}
Upvotes: 1