본문 바로가기

iOS/Swift

[iOS UIKit in Swift 4] section으로 나누어진 UITableView 만들기

section으로 나누어진 UITableView 만들기

UITableView를 2개의 section으로 나누어 iOS와 AOS의 버전을 나열해봤습니다.

section의 index는 0부터 시작하며 0번째 section의 header의 타이틀은 iOS, 1번째는 AOS 입니다.

section의 row는 OS 버전이 보여지고 있습니다.


numberOfSections에서는 표현될 section의 개수를 정의합니다.

titleForHeaderInSection에서는 section마다 표현될 title을 정의합니다.

numberOfRowsInSection에서는 section마다 표현될 row의 개수를 정의합니다.

didSelectRowAt에서는 선택된 indexPath에 대한 처리를 정의합니다.


아래 이미지와 소스코드를 비교해보시면 좀 더 이해하기 편합니다.

궁금하신점은 댓글로 달아주세요.


해피코딩 :)


Preview


Source

//
// SectionTableViewVC.swift
// UIKit component handling
//
// Created by Taehyeon Han on 2018. 8. 8..
// Copyright © 2018년 calmone. All rights reserved.
//
import UIKit
class SectionTableViewVC: BaseViewController, UITableViewDelegate, UITableViewDataSource {
// Define the array to use in the Table.
private let iOSItems: [String] = ["iOS12", "iOS11", "iOS10", "iOS9", "iOS8", "iOS7"]
private let AOSItems: [String] = ["9.x", "8.x", "7.x", "6.x"]
// Define the array to be used in Section.
private let sections: [String] = ["iOS", "AOS"]
lazy var tableView: UITableView = {
// Get the height of the Status Bar.
let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
// Get the height and width of the View.
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
let tableView: UITableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
// Register the Cell name.
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "sectionTableViewCell")
// Set the DataSource.
tableView.dataSource = self
// Set Delegate.
tableView.delegate = self
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Add UITableView on view
self.view.addSubview(self.tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Returns the number of sections.
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
// Returns the title of the section.
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
// Called when Cell is selected.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
print("Value: \(iOSItems[indexPath.row])")
} else if indexPath.section == 1 {
print("Value: \(AOSItems[indexPath.row])")
} else {
return
}
}
// Returns the total number of arrays to display in the table.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return iOSItems.count
} else if section == 1 {
return AOSItems.count
} else {
return 0
}
}
// Set a value in Cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "sectionTableViewCell", for: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = "\(iOSItems[indexPath.row])"
} else if indexPath.section == 1 {
cell.textLabel?.text = "\(AOSItems[indexPath.row])"
} else {
return UITableViewCell()
}
return cell
}
}


Github

https://github.com/calmone/iOS-UIKit-component


Reference