Yu On
Yu On

Reputation: 107

When to use numberOfSections or numberOfRowsInSection ? (Swift 3)

enter image description here

Why ? I don't know why the output gave me 3 sections only. I am very confused about numberOfSections and numberOfRowsInSection.

What I don't know:

Why the output returns 3 sections only?

What I know:

I saw a few post on StackOverFlow, they said Sections are like a group, and Rows are like the cells in the sections. But I still feel a little confused.

Upvotes: 4

Views: 6607

Answers (4)

Abhishek Mitra
Abhishek Mitra

Reputation: 3395

Well, func numberOfSections(in tableView: UITableView) -> Int // Default is 1 if not implemented and func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int is UITableViews Data Source method, numberOfSections is not required when you want only the same type of repeated list in your TableView, Like: suppose you want to show below in your tableview

1

2

3

4

..

in that case you will be required numberOfRowsInSection return 4 here your table will return 4 cells, if you not give numberOfSections this method it will be 1 by default and you code will work without having this method in your class.

But Suppose you need to show in your tableview that 1 2 3 4 A B C D It can be achieved by entering these data in an Array as string and print that in cellForAt.

But to make more it organige, what you can do here for better view to the user is to use section. Like

Number

1

2

3

4

Alphabet

A

B

C

D

Here You have to use numberOfSections and it should return 2, one for Number and second for Alphabet. and 1,2,3,4 and A,B,C,D are the rows under their section Number and Alphabet. I hope You got it, else comment below I'll try again to make you understand. Thanks

Upvotes: 8

Luca Di Michele
Luca Di Michele

Reputation: 76

I don't know if I can explain this better than the explanation you already found. Sections are groups of rows, the rows are elements of a section.

An object that can describe you visually a multiple section set of data could be:

let multipleSection = [["section1-row1", "section1-row2", "section1-row3"], ["section2-row1", "section2-row2", "section2-row3"]]

This array contains other arrays: each one of that subarrays are SECTIONS.

The content of each section are the ROWS that belong to that section.

I hope it helps and that I got it right what was confusing you.

Upvotes: 1

Nicolas Miari
Nicolas Miari

Reputation: 16246

UITableView presents its data in a two-level hierarchy: Sections and Rows. A section is exactly that: a way to group one or more rows that have something in common (what that means, will depend on the particular data displayed).

For example, assume your data rows consist of home appliances, and you want to group them according to in which room they are placed:

- Kitchen
  - microwave
  - fridge
  - toaster

- Bedroom
  - computer
  - lamp

Here, you have two sections (named Kitchen and Bedroom), and they have 3 rows and 2 rows respectively.

Or maybe you have a list of people grouped by nationality. Or cities grouped by state. How you use this two-level hierarchy, is up to you and what makes more sense for your data model.

Of course, you can always flatten the hierarchy by having only one section that contains all rows. This is the default behaviour if you don't implement or override the method numberOfSections(inTableView:).

Upvotes: 2

vadian
vadian

Reputation: 285059

Yes, it's a kind of grouping but don't mix it up with the grouped appearance of the table view.

Simple example: Look at the icon of the Contacts.app

enter image description here

The letters are the sections, the containing contacts are the rows in the section.

Your output reveals you have 1 section with 11 rows.

Upvotes: 3

Related Questions