Question

I was wondering whether it was possible to import static information, in my case an image, title and a short description - per item, in a UIViewControlller dynamically?

I'm (trying to) create an app that, for the current UIViewController I'm working on, shows the cooperating partners from the company in a list view. Each of them is shown as an item, containing an image (logo), the company name below and below that a paragraph of information about the company.

I was thinking of something like importing an XML feed (no experience with), such as

<object>
    <img src="/images/logo1.png">
    <title="Lorem ipsum>
    <description="lalalalalalalala"
</object>

<object>
    <img src="/images/logo2.png">
    <title="Lorem second one>
    <description="lalalalalalalala"
</object>

Something like this mockup

By simply importing such thing, I don't have to make up the whole page, calculate how long the UIScrollView would have to be and don't have to adjust it every time a partner gets added or removed. And it might be quite more clear than a whole view controller.

Was it helpful?

Solution

You have to divide the task into smaller steps.

  1. Read the data from an XML file using NSXMParser.
  2. Show the data in a UICollectionView.

The XML part is quite easy. There are many tutorials on the Internet that cover this. For example this one:

Second step can be more complicated depending on what kind of a layout you want to achieve. If it is a standard flow layout, the implementation will be fairly simple. Again, there is dozens of tutorials on setting up a UICollectionView. Example:

Alternatively, you can use UITableView to show your data. In this case, the implementation would be simpler than for UICollectionView.

More reading:

OTHER TIPS

Depending on where your XML feed/document is coming from. If you're just manually creating the document, and shipping it along with each version of your app, then all you need to worry about is getting the info from it; take a look at this tutorial, it describes some info-getting libraries. Take a look at UIImage for info on loading an image from a file. If you instead mean that you want to get information from some kind of remote, it seems like you have to accomplish three things:

  1. save a document including this various static info, so that it persists between app launches,

  2. load from that document to display the saved information, and

  3. keep this document up-to-date with some remote server (?) whence the most current version originates, in perhaps XML format.

First of all, let us assume that inside your application proper, you're going to have the data you want stored as some kind of NSObject. Check out this tutorial on how to save documents with your iPhone app. It explains fairly clearly how to take something like an array or dictionary (OBJC object) and store it. In the end, because your app is sandboxed you may not need to worry about the format in which it is stored. Whenever your application launches, and decides that it has current info, all it needs to do is load from that archived object and you're good to go. If you have some confusion about displaying the information in the UIView itself, refer to Apple's documentation.

You'll probably want to have a separate class altogether that handles keeping the data updated. This depends on your server's implementation. For example, your class could query your server for its info, determine with a hash whether it needs to get new data, and if so, pull down new data; use NSXMLParser as linked to above in order to parse the data, then save with the above method.

EDIT: regarding your worry about having to redesign the UIViewController every time you load, look into auto layout. You basically can create a nib file for your controller, tell it how everything will be in relation to everything else, then just assign values (i.e., images and strings) to all the fields and pow, you've got a consistent design.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top