1 00:00:00,420 --> 00:00:08,040 Swift if I prides itself for being able to allow developers to do things in a declarative way but also 2 00:00:08,040 --> 00:00:09,470 in a more simplified way. 3 00:00:09,630 --> 00:00:11,200 When creating user interfaces. 4 00:00:11,220 --> 00:00:15,930 Now one of the things if you are a mobile developer in general but specifically in Iowa's developer 5 00:00:16,380 --> 00:00:21,900 is that there is this thing called table view table view is essentially a way in which we can create 6 00:00:21,900 --> 00:00:27,060 cells on a list format so we can scroll up and down to show more information right. 7 00:00:27,060 --> 00:00:30,690 So imagine you are acquiring a database of some sort. 8 00:00:30,690 --> 00:00:31,830 That has a lot of data. 9 00:00:31,860 --> 00:00:36,990 So the best way to display that data would be to create rows in a list and so people can scroll up and 10 00:00:36,990 --> 00:00:43,920 down so we can see that information and also be able to tap on specific row and be taken to a details 11 00:00:43,920 --> 00:00:45,300 page per say. 12 00:00:45,300 --> 00:00:49,910 So in Swifty y Of course there is a better and faster way of doing this. 13 00:00:49,920 --> 00:00:54,840 So I'm going to show you right now how to put together a list or a table view for those of us coming 14 00:00:54,840 --> 00:00:58,310 from Objective C and you like it. 15 00:00:58,380 --> 00:01:01,250 Let me create a variable here called names. 16 00:01:01,250 --> 00:01:04,480 Just gonna be an array that will contain a few names here. 17 00:01:04,590 --> 00:01:07,680 So I'm gonna type a few names here right here. 18 00:01:08,030 --> 00:01:08,310 All right. 19 00:01:08,910 --> 00:01:10,080 That's simple enough. 20 00:01:10,440 --> 00:01:12,450 So have an array containing a few names there. 21 00:01:12,450 --> 00:01:16,160 So we are going to put all those in a list going to get rid of this. 22 00:01:16,230 --> 00:01:21,390 And of course we have in the actual component that we can use called list like this. 23 00:01:21,390 --> 00:01:27,100 You can see at the bottom here it's a container that presents rows of data arranged in the single column. 24 00:01:27,100 --> 00:01:28,620 So that's very handy. 25 00:01:28,620 --> 00:01:32,700 And we can go straight and pass in the data source as follows. 26 00:01:32,700 --> 00:01:41,130 So inside the parenthesis here I'm gonna just say self that name just invoke our array names that is 27 00:01:41,670 --> 00:01:46,320 and then what I'm gonna do here have to parse the I.D. because I want to make sure that each time we 28 00:01:46,320 --> 00:01:51,170 loop through and create this list here we have to pass this specific idea of each item. 29 00:01:51,170 --> 00:01:55,170 So in this case here it's like what we saw before with for each. 30 00:01:55,170 --> 00:02:01,440 We just gonna say dot self like this game because just a simple array and that should be fine. 31 00:02:01,440 --> 00:02:07,950 And then we'll say name in and of course again pass another view here this text which I mean I just 32 00:02:07,980 --> 00:02:09,990 add the name that we are passing. 33 00:02:10,020 --> 00:02:16,860 So in this case Ryan this we should see something very awesome here that was giving for free. 34 00:02:16,860 --> 00:02:22,780 Look at this just like that we have the names showing in a very nice list here. 35 00:02:22,800 --> 00:02:28,710 Now if if you've done some Iowa's development with objective c and or are you like hit Swifty you are 36 00:02:28,710 --> 00:02:31,210 you'll notice that to create a table of viewers. 37 00:02:31,230 --> 00:02:32,490 This is called at least. 38 00:02:32,670 --> 00:02:38,400 It's pretty cumbersome so you have to put together the actually view and then connected the rows and 39 00:02:38,400 --> 00:02:43,920 cells and figure out which item is being shown and so forth so just like that we're able to create a 40 00:02:43,920 --> 00:02:49,730 list and just pass in the data and then display the list as quickly as possible. 41 00:02:49,770 --> 00:02:50,700 Pretty neat. 42 00:02:50,700 --> 00:02:58,370 And of course as you know we can go ahead and add a few modifiers here when I make bold I say font to 43 00:02:58,440 --> 00:03:01,050 be maybe caption and so forth. 44 00:03:01,050 --> 00:03:06,390 So all of that stuff that we've learned we can of course apply to our textbook. 45 00:03:06,420 --> 00:03:08,910 That's a little bit too small but you get the point. 46 00:03:08,940 --> 00:03:11,080 Now our names array here is very simplistic. 47 00:03:11,100 --> 00:03:15,630 Usually we want to create something a little bit more sophisticated per say so it could come down here 48 00:03:15,990 --> 00:03:22,350 and create a struct and a car call this country which will represent a single country. 49 00:03:22,460 --> 00:03:28,910 And of course usually when you create data types you want to put those inside of their own data folder. 50 00:03:28,950 --> 00:03:33,700 But in this case because we're just trying to get things across here it's OK to create a struct here. 51 00:03:33,780 --> 00:03:35,790 Sometimes it's actually necessary to do so. 52 00:03:36,360 --> 00:03:48,200 So in this case here I can say for instance let name be stuff String let population be of type and then 53 00:03:48,230 --> 00:03:51,470 when I say left I.D. it's going to be of type account. 54 00:03:51,570 --> 00:03:51,840 Right. 55 00:03:51,840 --> 00:03:56,550 So we're going to go in and create an actual data type of type country so that we can populate everything. 56 00:03:56,580 --> 00:04:05,130 So at the top here we can say let country list of course you're going to be of type country objects 57 00:04:05,550 --> 00:04:06,030 or types. 58 00:04:06,030 --> 00:04:06,960 There we go. 59 00:04:07,080 --> 00:04:12,480 And I'm going to go ahead and create the actual array because since we're here might as well do that 60 00:04:13,110 --> 00:04:15,410 I'm going to fill out this array with country. 61 00:04:15,510 --> 00:04:23,130 So in this case my passing I.D. start with zero and string let's say Mozambique population we're going 62 00:04:23,130 --> 00:04:26,410 to just put a gibberish number here doesn't really matter. 63 00:04:26,670 --> 00:04:28,590 And we're the same thing for others. 64 00:04:28,590 --> 00:04:34,380 I already have a list of countries so I'm gonna just copy them and paste them here to simplify our life. 65 00:04:34,380 --> 00:04:37,150 So now we have our country list and we're going to use that. 66 00:04:37,620 --> 00:04:41,760 Now one thing to remember here is that this country type that we created at the bottom here we need 67 00:04:41,760 --> 00:04:47,240 to actually make it confirmed to identifiable so identifiable as such. 68 00:04:47,250 --> 00:04:52,080 The reason why is because with our list if we don't add this as identifiable then we're gonna have some 69 00:04:52,140 --> 00:04:57,880 issues looping through and adding those items or these countries in our table for you list there. 70 00:04:59,240 --> 00:05:05,600 So at this point here we'll just change a few things so instead of call these names gonna call this 71 00:05:05,600 --> 00:05:12,800 country list as say country list as such and instead sort of self I can just go ahead and actually get 72 00:05:13,100 --> 00:05:17,310 the I.D. because each country has its own I.D. And we're going to use that. 73 00:05:17,310 --> 00:05:17,530 OK. 74 00:05:17,540 --> 00:05:21,880 Now we have a problem here because says requires that a country conforms to string. 75 00:05:21,890 --> 00:05:28,340 What he's saying here is that this name as it is right now name is an actual object of country. 76 00:05:28,340 --> 00:05:33,550 So we need to go and invoke the property name or whatever else we want do. 77 00:05:33,680 --> 00:05:37,040 This gets a lot changes from name to country to be more specific. 78 00:05:37,070 --> 00:05:40,010 Which means I also have to of course change the handler there. 79 00:05:40,010 --> 00:05:41,750 So a country like this. 80 00:05:42,530 --> 00:05:43,160 All right. 81 00:05:43,160 --> 00:05:44,350 So save this. 82 00:05:44,350 --> 00:05:45,270 Give it a run. 83 00:05:45,290 --> 00:05:49,340 We should be able to see the list of at least 10 countries. 84 00:05:49,670 --> 00:05:52,280 And while I can see it's looking real good. 85 00:05:52,910 --> 00:05:53,180 OK. 86 00:05:53,240 --> 00:06:00,470 So just like that we're able to create a very simplistic list from first to having just a simple array 87 00:06:00,560 --> 00:06:06,110 and then creating an actual country list which contains objects of countries which contain these names. 88 00:06:06,110 --> 00:06:07,370 And of course population. 89 00:06:07,580 --> 00:06:12,080 So in the next video what are we gonna be doing is we're going to make sure that we add in navigation 90 00:06:12,080 --> 00:06:13,610 to our list here. 91 00:06:13,610 --> 00:06:17,870 So when we click on one row you should take us to somewhere else or a detailed space.