How do I "connect" a table view to a view controller
Alright, I know this is a vague conceptual question, but I really need help here. Thanks in advance if you decide to take the time to read this. I would never even consider writing this much except this is such a great forum with so many helpful people I thought this would be the best place to ask.
This is all related to the question here (you don't have to look at it - I explain everything below): Pass parameter when initializing table
I've been working for days on the same problem, but I'm realizing there must be something big I'm missing. I've googled and googled and I even bought (and about 50% read) two Obj-C books a few days ago, but I'm still grasping at something that seems like it should be incredibly easy. I'm obviously pretty new to OOP, but I have mediocre skills in HTML, perl, sql, python as well as some of the ancient stuff like pascal and basic. I'm a n00b, but not a moron (well, actually this experience is changing my mind a bit).
Anyway, my end goal here is to simply create an app with 8 "normal" buttons on the first view (level 1), each doing basically does the same thing - which is to show a simple table view (level 2) with data in cells that can be clicked to continue to drill down to details (level 3). Very basic and straightforward concept. The only difference between the 8 possible level 2's is the data that will be shown. I've already constructed sql queries that work just as I want for each button.
So, here's where I stand: I have a perfectly working app that does everything from level 2 down exactly as I expect - the queries work, the tables are beautiful - so that's great.
Also, I have another nav-based app that launches on "level 1" and shows me 8 buttons (I hide the nav bar on level 1). If I click any of the buttons on level 1, the level 2 view (which is a nav bar + a table) slides into view exactly like I want. The problem is the table is just blank. No matter what I do, I can't get the level 2 in the second app to show me the data, even though I can show all of that data in the first app perfectly. For the life of me, I can't figure out how to "link" level 1 with level 2.
Hopefully you can understand this gap I'm trying to bridge. Since there are 8 possibilities for level 2 (with only very slight differences in sql queries on the same sql table), I initially tried coming up with a way of "passing" an integer to the level 2 view (in the first app) and then selecting the sql query based on what was passed (see the link above for that fiasco). Once I got that working, I planned to figure out how to make the buttons do the "passing" later. However, after about 16 hours screwing with that, I just gave up and decided to make 8 different table view controllers, all with nearly identical code except the query. That way, if I could just get a SINGLE button on level 1 to simply push to just ONE of the level 2's with NO parameters I would be a horrible but successful programmer.
Unfortunately, even that hasn't worked out for me. I have tried every possible control-drag and window/view/table combination I can think of in Interface Builder, but no matter what I try, the data never loads into the table view, even though it works great in my first app. I have gone through every line of code - they are the same except something has to "call" or "launch" the level 2 part and I'm just not getting it.
So, I'm going to break with convention/expectations here and not post any code in my question. I just want to know - how can this possibly be so difficult?? I am very analytically minded and I catch on quickly, but I have to say I have never been so humbled by a technical challenge in my life.
Can anyone explain to me, at a conceptual level, what I need to be doing here or what I'm missing? Even if you give me a link to something to read I would appreciate it very much. I have watched tens of hours of tutorials on youtube, but I'm always up for more.
Of course I'm willing to share my code, but there is so much of it and I'm so new at this I really don't know where the relevant parts are. Plus, I actually want to learn how all of this works so I can help others. If there is such a thing as PM on here I'll email it to you if you're willing to take a look. Once I get it working, I will post the code here. I have to believe there are other people looking for the same kind of thing as I am. However, more importantly, I just want to know, from a high level, what is the correct way to approach my problem? If you look at my link you can see what I've been trying (which was to pass an integer to the method that populates the table), but as I said, I basically gave up on that because I wasn't getting anywhere. People are trying to help me, but I'm an idiot.
Thanks for bearing with my agonizingly long message. If you made it this far and have some suggestions for 开发者_运维技巧me I'm all ears. I'll be honest, though - if you tell me I should just scrap the whole thing and use core data I'll cry. I really don't think I have the time to figure out a whole different way of managing data. As I said, I'm pretty happy with the database and the query parts of my app - it's just managing the freaking views and passing data between them that is killing me!
Any help is appreciated - thank you all so much.
If I understand your question correctly, you are asking how to initialize a view controller and pass in some data to alter its behavior. The key concept here to understand is how objects are initialized in Objective-C. One of the most common questions that developers who are new to iOS have is:
How can I pass data between my views?
Yes, eight different links there. (Okay, that eighth link is a little bit off topic, but it's close enough.) There are several ways of doing this and I'll go through them briefly. I'll also describe custom initializers, which are a relevant point as well.
Let's pretend we were building a catalog application which shows a bunch of products in various categories. Imagine that our app opens to a list of products, much like the Apple Store App. Say that when the user taps on a product we want to show a product page.
You can set properties on the "next" view controller. - Simply, we can create a
UIViewController
subclass and set theproductID
property (which we made up). Let's call our newUIViewController
aProductPageViewController
. Here's how this would look:- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath //Create a ProductPageViewController ProductPageViewController *ppvc = [[ProductPageViewController alloc] initWithNibName:@"ProductPageViewController" bundle:nil]; //set the property on our ProductPageViewController [ppvc setProductID:42]; //We would usually present the PPVC here. //After presenting, remember to release the view controller }
In the first line, we create the product view controller. We call
alloc
, theninit
. (The functions are wrapped - that is, we callinit
directly on the result of thealloc
method.)Then, we set a property of our view. The view can now be configured in
viewWillAppear
and all is well.You can share the data through a persistent store. - This method works a little differently. The view controllers don't communicate at all, except for the first one presenting a second one. Whenever a value in the first view changes (that you want to persist), you write it to Core Data or
NSUserDefaults
. Then, the new view reads the value as it needs it.In your first view controller:
//A method to store the data - (void)storeData:(id)pageID{ [[NSUserDefaults setObject:pageID forKey:@"pageID"]; } - (void)showNewPPVC{ ProductPageViewController *ppvc = [[ProductPageViewController alloc] initWithNibName:@"ProductPageViewController" bundle:nil]; //Show and then release the PPVC }
You can use custom initializers. - This is probably the most intuitive way to do it, once you understand the concept, because this is the only one where data is actually "passed". (As opposed to method 2 where no data is directly shared and method 1 where data is passed as a property.)
Notice that in the earlier examples, I used the
initWithNibName:Bundle
method. You might also notice thatUITableViewController
s use a different initializer,initWithStyle:
. Those two initializers take in some information for the new object so that it knows how to load. Let's look at the first one first:- (id)initWithNibName:(NSString *)nibNameOrNil Bundle:(NSBundle *)bundleNameOrNil;
The first argument tells the view controller which nib file to load up. I'm going to ignore the second argument for now, since I've never seen anything passed in except
nil
. Moving right along to the second example:- (id)initWithStyle:(UITableViewStyle)style;
You can pass in one of two
UITableViewStyle
values here. This is one way to define the style of a table view (the other way being to modify a nib file directly).Let's extend this concept a bit to our example. I'm now going to show you how to make your own custom initializer. Let's initialize our ProductPageViewController instance:
- (id) initWithProductID:(int)productID;
That's simple enough. Now, we need to implement the method and actually do something with the product ID. We'll start with the barebones code here, required to "mimic" the functionality of the default initializer.
- (id) initWithProductID:(int)productID{ self = [super init]; return self; }
This method will return an initialized copy of our
ProductPageViewController
, however, it won't load up our UI from a NIB yet, or if this were aUITableViewController
, it wouldn't set theUITableViewStyle
. Let's work with a NIB first and then I'll show how to work aUITableViewController
. So...- (id) initWithProductID:(int)productID{ self = [super initWithNibName:@"ProductPageViewController" Bundle:nil]; return self; }
Now. we have an initialized
ProductPageViewController
, loaded from a NIB, but it doesn't do anything yet. Notice how we don't expose theNibName
andBundle
arguments, but we just pass them in ourselves. If you want, you could theoretically expose those too. Now, let's take thatproductID
and do something with it.- (id) initWithProductID:(int)productID{ self = [super initWithNibName:@"ProductPageViewController" Bundle:nil]; if(self){ self.prodID = productID; } return self; }
With our latest changes, our "
PPVC
" now knows about theproductID
. It can query the database as you want and do things with the results. You can then run different queries based on thisproductID
.
Two More Quick Tips:
Perhaps you want to pass in several arguments. Of course you can simply add them to them method signature
- (id) initWithProductID:(int)productID andCategoryID(int)categoryID
, but what happens if you have five, six, or fifty six (yea, that's a lot) arguments? I'd advise passing in a collection or array of arguments.To use custom initializers with
UITableView
, you pass in aUITableViewStyle
instead of a NIB name. Here's what it might look like:- (id) initWithProductID:(int)productID{ self = [super initWithStyle:UITableViewStyleGrouped]; if(self){ self.prodID = productID; } return self; }
When making your subsections, I'd suggest a combination of persistent data and custom initializers. I also advise taking a peek at the viewDidLoad
and viewWillAppear
methods.
精彩评论