Note how to use Swift super basic TableView

  1. Add TableView in Storyboard Drag from ViewController to DataSource and Delegate

If you can drag it, it looks like this スクリーンショット 2020-11-06 14.45.08.png

  1. Add TableViewCell inside TableView Name the Cell at the Identifier

I usually named it "cell" スクリーンショット 2020-11-06 14.47.39.png

  1. Source code (basic) 3.1 Protocol added In the view controller where the TableView is located Add UITableViewDelegate and UITableViewDataSource protocols

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)
            }
        }
    }
  1. Source code (AddOn) 4.1 Display the session index curler ↓ Just put this in, but by default the index character is Blue and the background color is Clear

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
    }

Simulator Screen Shot - iPhone 8 - 2020-11-06 at 16.28.17.png

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

Note how to use Swift super basic TableView
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to use Tab Bar Controller
How to use Java Scanner class (Note)
[Note] How to use Rails 6 Devise + cancancan
Ruby: CSV :: How to use Table Note
[Swift] How to use one option alert
How to use Swift's Codable Super personal memo
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use Segmented Control and points to note
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
[For super beginners] How to use autofocus: true
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Swift] How to get the number of elements in an array (super basic)
How to use GitHub for super beginners (team development)
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to introduce Basic authentication
How to use @Builder (Lombok)
How to use java class
swift CollectionView Super basic usage
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation