If you can drag it, it looks like this
I usually named it "cell"
HomeViewController
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
3.2 Data creation
HomeViewController
let sectionTitle = ["fruits","Many mountains of","drink"]
let listContentOne = ["Apple","Strawberry","grape","Pineapple","Kiwi"]
let listContentTwo = ["Tateyama","Mt. Nantai","Mt. Tanigawa","Mt. Kinpu","Hotakadake","Daibosatsu Mt."]
let listContentThree = ["coffee","Soy milk","juice","Carbonated","water"]
3.3 Determine the number of sessions
HomeViewController
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitle.count
}
3.4 Determine the session title
HomeViewController
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitle[section]
}
3.5 Determine the number of cells in the session
HomeViewController
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return listContentOne.count
case 1:
return listContentTwo.count
case 2:
return listContentThree.count
default:
return listContentOne.count
}
}
3.6 Put the contents in the cell
HomeViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
switch indexPath.section{
case 0:
cell.textLabel!.text = listContentOne[indexPath.row]
case 1:
cell.textLabel!.text = listContentTwo[indexPath.row]
case 2:
cell.textLabel!.text = listContentThree[indexPath.row]
default:
cell.textLabel!.text = listContentOne[indexPath.row]
}
return cell
}
3.7 Implemented behavior after pressing a cell (only one example)
HomeViewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if indexPath.row == 0 {
let anotherStoryboard:UIStoryboard = UIStoryboard(name: "CellDetailPage", bundle: nil)
let targetViewController = anotherStoryboard.instantiateViewController(withIdentifier: "celldetail")
self.present(targetViewController, animated: true, completion: nil)
}
}
}
HomeViewController
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return sectionTitle
}
4.2 Color, change background color
HomeViewController
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
tableView.sectionIndexColor = UIColor.darkGray
tableView.sectionIndexBackgroundColor = UIColor.clear
return sectionTitle
}
Reference article: [[For beginners] How to use Section (section) of UITableView](https://blog.mothule.com/ios/uitableview/ios-uitableview-section-basic#section What is a section and what is a row row)
Recommended Posts