Tip:
Highlight text to annotate it
X
so i have a single view application right now there's no change from the
default project type i'm going to go into the excited defile
and from my object library just going to filter on the word table
i'll see three things here at the table view the table view sell on the table
view controller
i want to be table view
already have a view controller
i'm going to click that guy
drag it across and make it phil this screen
but look at my apt to be its inspector
does not and all full up we can do we can
change a few things are at the table be like
whether it's plain or groups and we'll see them a little while
the important thing i need here
is in the connections and specter deal last part of the inspector panel
my two outlets for datasource and delicate
and both of these
i'm going to hook up to file's owner so we can just write the code
in our view controller class
datasource over the files on a
and elegant over the files on a
what i want to do is populate this table view
with some data that's actually stored in a final right now
i have a file called
courses dot t_ o_ s_ which is on my desktop
you can get it in the exercise files or create one yourself
some then to grab courses dot p list and i'll talk about this in a moment i'm
just going to drag it over into my project i'll put it in the supporting
files folder it doesn't really matter where it goes as long as it's in the
project
now when this window pops up
that's very important that we choose to
copy of the items into the destination groups folder if needed
so that we have uh... own copy all things not just linking back to on the
desktop
we want to keep a la fast together
so i have courses dot p list here this is a property list file
and all i have in it
is a list of the few
courses from linda dot com are related to ohio s
and their offers
you can of course creator property list all yourself if you go to the xcode
new file menu
one of the options that you'll find in the resources section
is a property list file
creating this new don't have to understand a great day
dummy one here
would give me this p list file with keys and values and all have really done
as do the equivalent of putting in a course title
and cab over
and do off a goes here
hit return a couple of times to get a new one
and than anything more complicated than creating multiple entries like this
i'm going to delete that it was file because i don't need it
she's to delete the whole thing
the only thing to bear in mind here
is that i'm using these as keys and values and keys need to be unique
so we can't have a duplicate entry in this left column
although it doesn't matter if i have duplicate entries on the right
it's the key is the important part
so i want to load this file
into application when it's running
had to do this on first and the jump over to my view controller dot h file
because i'm going to define a couple of objects that can hold this an occupation
first i'm going to find an s dictionary object which will hold a multiple keys
and values the course name and the office
event
because the dictionary doesn't have a and numeric indexes all based on the
keys
it would actually be quite useful in this example
for me to have one so i'm going to create a simple anissa rate
so that i can make a numeric index for this
i want to make it in a moment is a zero-based array
that will just contained all the keys for that dictionary
so jump across to the implementation file because here i do need to
synthesize
both of those properties
going to go ahead
and drop down into
if you did little work here
to run some code to load in that file
first thing i need to do
is construct a string that has the path to the far the question as well
how do i get to that mo here's how
split that a bit
and what i'm doing here is using this enhanced bundle main bundle method
called
this is really a way of getting to the install filesystem on the i_r_s_ device
when i don't quite know what it is
this line might look a little strange it's really a way
of obtaining that
courses keyless faro
that's part of our internal bundle internal packaged applications
we don't have
direct access to the file system on and i a west of i said this is the way
we get hold of our own accents
well next they need to create a bandanas dictionary that i've already defined
was called courses
and also that equal to
tennis dictionary
will do a normal alag
but i'm going to initialize
and the useful
method i have is this one
in it with contents to file
and i'm going to diffuse the
my file string that i created one twenty six
so really we're just
grabbing the path to that file unloading into the m_s_ dictionary
for the next thing i'm just going to do is from that dictionary i'm going to
create that coleraine too so i have a numeric based way of getting to all the
keys in the dictionary
heading to the very easily
but just calling me all keys method of the n_ s_ diction
that's all i needed to load the file
and create miami's picturing him i_n_s_ array
i've got all that information in my apt and it's not doing anything with it yet
well in are interface file we already decided that
our table viewers hooked up
two files on a as the datasource indelicate but what does that mean
formally speaking what i should do in this class definition
is a volunteer and say we are now a table view datasource
comma and as you like
table view delegate
when one of the best reasons for doing this
is just so we can
option click on them and actually lead to either the protocol reference or just
to the header file
either way would work
and it's going to tell me that if i'm volunteering to be a you are a table
view datasource
there are a couple all the required a methods that i must implement
which is this one
table view toll on a number of rosen section
and this one
table view call on sell for row at index path
i'm linda graham this whole block here and between
at sign required that sign optional
jump back over into my implementation
and i'll paste it couple of lines after my synthesize message
going to just get rid of that comment so we can read this a little better
and of course i discovered a declaration so need to add some curly braces on both
of these
method signatures to make sure i can add my code
source this first one here
table view cullen number of rosen section
so how many rose are going to be in the table view
all that all depends
how many entries are in that courses p list well
well they're going to be loaded into the n_ s_ picturing so i really need to do
has returned
courses
this next method is really the important one
table view car loan sell for rozak index path
this method will be called multiple times
and every time it's going to pass in
a new index position zero then one and two then three
we need to build to create the contents
of each row and passive back if you noticed that's what we have to return as
you are a table view cell
this is our job as the datasource
i'm going to go ahead
and first create as you are a table view cell
this call itself
we alicante i'm going to use one of the more interesting and that's which is in
it with style
what does this mean
with a different table view cells can have different styles this is an
admiration so if i start to write their regular pops up here you i table view
self-styled default if you like table you sell style
subtitle
i'm just gonna go with default right now will see the other ones latest
and then that we have another argument here reuse identifier
george exception in a string
right now i'm just going to give it a string of cell all lower case
what we're really doing is
taking this new object this new cell with a name
and the reason why you might use that we use identifies you have
several different kinds of cells in your application some with images some
without some with some titles some without
more see this into the next movie
we're only going to have one kind of basic cell
so we can call it whatever we want i could say sell i could say fred i could
say a_b_c_ it's just attack
so the cell is now created
but we're not doing anything with it yet unpigged give it some kind of value some
piece of information
i'm going to create a new kind of string object
that holds a course next
i'm going to go up and grabbed that
from the array that we created a little earlier
this is a
zero-based array
that's going to contain
all the names of the courses that i've pulled it out of the n_ s_ dictionary
the questions
how do i know which position to go to well it's been passed in to this method
in the
self perot at index path
gives me an index path parameter that i can use to get to that
megan if your parcel about
how is this method being called
well don't worry about at the table view is calling it automatically because
we're the datasource so this method it's called multiple times
passing in
different row numbers everytime we just have to grab
the row number and use it
some bendigo internet array using object index
and i'm using the index path parameter the guest pass into this method
but not just index path
just a part of it it says
and xpath
dot ro
alternatively here again amusing
dot syntax that i could also use a square brackets and just say
index past
space
and access the property that way
we should now have a strain
and what i'm going to do is set
the text label
on that sell objects we've just created
and said it's taxed
to the current course name
finally
this method needs to return you aren't able to use l well we created one we
change the text on it
i'll say returned self
say that no more find out if i missed anything
looks pretty good actually
we've got the table view it scroll lable it's loaded with data
technically it's a little inefficient right now i'm going to talk about proper
ways that we should be creating those cells
there are more recommended techniques for doing it
but this is the general process
we had a table view
we hook it up to a datasource
we provide the required methods to say
how many rose does it have and wanting each row and we were trying to sell
for each different rotary half