Displaying Array Data


TableView: Landmark Book
7m 54s

The course is part of this learning path

Start course

This course is going to explore table views, which are views that allow you to display data in your arrays. Then we're going to put this knowledge into practice by building an app called Landmark Book.

Intended Audience

This course is intended for beginners who want to learn how to build apps for iOS.


To the most out of this course, you should have some basic understanding of programming and computer science in general.


Hi. With in this lecture, we're going to see how we can display those values in our table view using this table view functions that we have created before. So, right now I'm just displaying tests for 10 times. So, how many data do I have in here to display in my table View?

I have five datas. Right. So, Colosseum, Great wall Kremlin, Stonehenge and Taj Mahal. So, I can easily come to my number of rows in section and I can just say five and it will work as well, but I'm not going to go for that because there is a better way to do this. I'm going to go for landmark names dot count. So, we have seen this before. Right. We have dot count method in our landmark names actually in all arrays. So, if you come over here and say landmark names, it will give you the array and if you say dot count, it will give you the number of elements in that array. So, later on we may decide to add new landmarks. So, it's wise to use landmark names dot count in here rather than just saying five, but I cannot reach that right now. How can I do that? Of course, we have to define this variables outside of the viewDidLoad but inside of our class. So, that we can reach those variables from all the functions inside of our class. So, you know how to do that. Right. So, you just can copy this or you can just even cut this with command x and paste it in here and then later on, I'm going to do the same thing for this one as well. Cut this and paste it in here. So, right now I can reach those values from everywhere. So, here I can change landmark names. I can append new values. Let me delete this as well. And here we go. So, we can reach landmark names, landmark images from here. And let me create a note here saying that these our landmark book data. Okay. And then later on, I can come to my number of rows function. I can call landmark names dot count. So, this is landmark names dot count and it will return five to me, anyway. But it would be much more practical if we want to add some more data here, later on. So, here we go. We have our number of rows in section function already.

Now, we have to find a way to display those names in our cell.textLabel. So, rather than saying text, I'm going to refer to my landmark names. So, let me run this and let's see what we get. We're going to get test five times. Right. because we're creating five rows in here. But we're just displaying test in those rows. Rather than displaying this test, I'm just going to display the elements inside of my string list one by one and we have indexes in order to achieve this result. As I said before, table view has its own index and a race has its own index as well. So, if you go over here and say landmark names, then you can choose an index to show in related rows. And there is a very good way to do that. If you open a parenthesis in here, you can just go for index path that is given to you in this function. So, this is the current index that we see in the row. So, this is zero for the zero,  one for this, two for this, three for this and it goes on and on and on. So, it has it's own indexing algorithm and it has it's own indexing logic and it overlaps with the logic of array in the same place.

So, that's why we use table view with arrays because it's so efficient. If you come over here as a indexPath.row, it will just give the related role to you in each related array elements. So, that we will get Colosseum first, Greatwall second, Kremlin, Stonehenge and Taj Mahal. So, that's very good. Right, that's very cool. So, that's it actually. That's all we have to change in order to make this work. So, we're calling landmark names and we are saying indexPath.row in order to get zero, one, two, three, and four in order. So, here you go. Now, we see the Colosseum, Great Wall, Kremlin, Stonehenge and Taj Mahal here. So, that's good.  Right. Now, we have learned how to display those arrays inside our table view, but we don't know how to make them clickable. Right. So, what we want here is actually to let user click one of these rows, one of these cells so that we can pass this data to another review controller in which we can show the details. That's what we're going to do with in the following lectures.

About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics