I have a UITableView
set up in storyboard with static rows populated with different content, and all going to a single UIViewController
. I've also got a separate plist for each view I intend to load (containing the different content), that I would like loaded depending on which row was selected.
As a bonus, I would rather have a single plist that the UITableViewController
will populate the rows with (instead of having 120+ static rows in storyboard), but am unsure of how to do that as well.
I've searched quite a bit, but only found things for Objective-C. I'm a bit new to Swift, and never really got far into Objective-C, but I think I've got the basics down.
Right, my question. My question is, how can I load each individual plist (one for each row) and display its contents when the UIViewController is shown? My bonus question is: how can I consolidate my 120+ static rows into a single plist that basically accomplishes the same thing, except instead of editing the SB to add new rows, I just need to add to the plist?
First of all, it's rare to use plist to store such contents. Not easy to maintain. Use XML or json to store it are better choices.
Regards your question #1,
put below code to a loop to load your plist into a dict like this:
You may want to store all the dicts into an array, so you know how many plists you have. Let's say we have
dictArray
property in your view controller to hold all your dicts.You don't add static cell on SB for UITableView, it's not efficient. You just let the dataSource delegate to load the data and create as many cells as it needs.
I will leave some code for you to write, this will warm you up. I just give the essential part of code.
Don't forget to set your dataSource and delegate to proper object, like
self
!then,
Some ways to pass data to your new UIViewController:
If you segue exist in the storyboard with a segue identifier between your 2 views, you can just call it programmatically using:
later you can access yourDataDict in your
nextViewController
to load itYou could also do :
Or if you are in a Navigation controller :
To consolidate your plists into one plist, you might need to write some small programs or scripts to merge them; or copy them. There's a quite a lot of ways to do it. But as I suggested, I don't recommend you to continue using plist to do such things. You better change to another form at beginning. Otherwise when you realize you really need to change, it's too late and you have many code to rewrite.