Home>

I am making an application using tableView, besides the official documentation of apple developer,
I refer to the following site.

"Let's use TableView with Swift"
https://qiita.com/pe-ta/items/cafa8e20029047993025

In the "Set TableView Outlets" in this
It connects ViewController with dataSource and delegate of Outlets.
(1) I understand that without doing this, the code will not be reflected in the tableView,
What does that mean?
② How would you describe it with a code?

Also, in "Setting the Identifier of TableViewCell", I can see that this is also the act of connecting the code and tableViewCell, but ③register(_:forCellReuseIdentifier:)What code should I write if I use?
Since the class is required with the first argument, is it necessary to create a class corresponding to cells from 1?

I would like to know how to express it with a code for later learning,
In order to make it easier to look back later when creating an application,
I would like to use the code area instead of the Xcode Utility area as much as possible.

Thanks for your cooperation.

  • Answer # 1

    (1) I understand that without doing this, the code will not be reflected in the tableView,
    What does that mean?

    If you do not set delegate, dataSource, you cannot set the calculation of the number of cells to be displayed after displaying the frame of tableView, the display of individual cells, the behavior when selecting cells, etc. Even if it is set with, it cannot be reflected.

    By the way, many of the questions that "the contents of UITableView and UICollectionView (the contents of cells) are not displayed" in stackoverflow are missing settings of these delegate and dataSource.

    ② How would you describe it with a code?

    Past questions

    https://stackoverflow.com/questions/242391">I get an error in cellForRowAt of TableView.

    I saw you.
    It will be as set in the code presented there.

    To be specific, the outlet to tableView

      @IBOutlet var tableView: UITableView!

    If you setviewDidLoad()Etc

      tableView.delegate = self
        tableView.dataSource = self

    I think there are many ways to describe.
    However,selfThe part of may set different instances depending on which class sets delegate or dataSource.

    Also, in "Setting the Identifier of TableViewCell", I understand that this is also the act of connecting the code and tableViewCell, but when using ③ register(_:forCellReuseIdentifier:), what kind of code should I write? Is not it.
    Since the class is required with the first argument, is it necessary to create a class corresponding to cells from 1?

    This is also a question from the past

    https://stackoverflow.com/questions/242391">I get an error in cellForRowAt of TableView.

    You have set yourself in the code presented in.
    Where

          tableView.register(UINib(nibName: "ShopCell1", bundle: nil), forCellReuseIdentifier: "ShopCell1")

    Since it is set like this, it will be described like this.

    By the way, in the above case, it is a way to set a custom cell with xib.
    If you want to write a custom cell using Table View Cell, the above settings are not necessary. Set the custom cell and Reuse Identifier in StoryBoard.

         let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

    When creating a cell with the feeling like (specificallytableView(_:cellForRowAt:)Will be set).