PDA

View Full Version : Cells in iOS app




bkribbs
Feb 16, 2012, 06:11 PM
Hey guys, I am trying to make a very basic app that has a list of roads, and certain exits of importance to me.

I followed a tutorial, so I see how to make it so that I have several cells with different items.

However, I cannot figure out how to have multiple roads, that all open to different pages.

I can add them in the story board, but as far as I can tell, the cells in storyboard really don't matter.

So basically, I need to figure out how to have multiple cells that point to different pages.

Any pointers?



ArtOfWarfare
Feb 17, 2012, 12:57 PM
Isn't it just a matter of adding cells, then control dragging from the cell that is being pressed on to the view that should be shown when the cell is selected?

(I don't really know, I haven't looked at storyboards in over a month.)

xStep
Feb 17, 2012, 01:10 PM
Your post is confusing. It doesn't help that you haven't posted any code. Also note, I have no experience with the story board, so I won't mention it further here.

The typical way table view clicks work is to cause a common event, like going to a common screen that handles a common scenario. I'll get back to this in a moment.

To fill those cells, you read from an orderer list which you take advantage of in the delegate tableView:cellForRowAtIndexPath: by using indexPath.row to access an entry of the list.

To take action on a cell, when delegate tableView:didSelectRowAtIndexPath: gets called, you use indexPath.row to reference back to the original list for further processing or another related list with an appropriate matching order.

Now that you are processing the click, you take some action. Often that is a push to another UIViewController screen. This screen would be written to display a common set of data and perhaps do other related things. I mention that, because your comment "that all open to different pages" suggests you want to use different UIViewControllers for each road. Without knowing more details, that sounds like a terrible idea.

This thread (http://forums.macrumors.com/showthread.php?t=1320343) discussions table views and the mistake of many UIViewControllers where few, or one, is needed. Reading that may allow you to understand why I say 'terrible idea' above.

I suggest you get a good book or two. Learning programming by jumping around web tutorials doesn't seem like a good idea. You need a good foundation first.

I just found UITableView Basics (http://nsscreencast.com/episodes/3-uitableview-basics?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Nsscreencast+%28NSScreencast+Episodes%29). I haven't looked at it.

dejo
Feb 17, 2012, 01:16 PM
I followed a tutorial...
What tutorial? Be specific.

Also note, I have no experience with the story board, so I won't mention it further here.

Storyboards have the ability to use "static" cells, something which I suspect the OP may be using. They add a whole new approach to populating and interacting with a tableView.

bkribbs
Feb 17, 2012, 02:02 PM
Thanks for the help so far guys.

The tutorial I followed was this: http://www.techotopia.com/index.php/Using_Xcode_Storyboards_to_Build_Dynamic_TableViews_with_Prototype_Table_View_Cells#Adding_the_Table View_Controller_to_the_Storyboard

But is doesn't account for multiple cells that lead to different pages.

Adding cells in storyboard doesn't just add them in the app, unless I am missing some setting somewhere, you still have to add them in manually.

dejo
Feb 17, 2012, 02:07 PM
See Part 2 (http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2) of the "Beginning Storyboards in iOS 5 Part 2" tutorial for more info on using static cells with storyboards.

ArtOfWarfare
Feb 17, 2012, 02:37 PM
But is doesn't account for multiple cells that lead to different pages.

One thing I think you might be failing to realize is that it's possible that you want all your cells to lead to a single "page" (I think you mean "view",) and to just change the content of the view based on which cell was picked.

You said your app should present a list of roads, and when the user selects a road, it should lead to a list of exits. Yes, you could hardcode the views, but the "correct" way of doing it would be have one view with the roads, and then a second UITableView that loads a list of exits for whichever road was chosen.

dejo
Feb 17, 2012, 02:47 PM
My apologies to the OP. I've been stressing static cells when it's pretty clear you need dynamic cells, which are populated and whose interactions are set up via code, just like everybody has been leading you towards. Sorry.

bkribbs
Feb 17, 2012, 03:59 PM
One thing I think you might be failing to realize is that it's possible that you want all your cells to lead to a single "page" (I think you mean "view",) and to just change the content of the view based on which cell was picked.

You said your app should present a list of roads, and when the user selects a road, it should lead to a list of exits. Yes, you could hardcode the views, but the "correct" way of doing it would be have one view with the roads, and then a second UITableView that loads a list of exits for whichever road was chosen.

Ah. That sounds exactly right to me.

So I have the cells all point to a single view, and the contents differ by which cell was selected.

So I would do that through the UITableView? Is there an example I could look at?

PhoneyDeveloper
Feb 17, 2012, 06:28 PM
You might want to start with Apple's TableViewSuite sample code. Also read the TableView programming guide.

There are a huge number of UITableView tutorials and examples on the net. I recommend you look at Apple's sample code and documentation first.

bkribbs
Feb 18, 2012, 07:30 AM
Alright I'll have a look at that. Thanks!

bkribbs
Feb 19, 2012, 01:30 PM
Ok guys, I'm now on the right track!

But now, I have this:

#import "RoadItemViewController.h"

@implementation Tab2_ItemViewController

@synthesize selectedIndex, selectedItem;

- (void)viewDidLoad
{
[super viewDidLoad];

[outputLabel setText:selectedItem];
[outputImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", selectedIndex]]];
if ([[outputLabel] isEqualToString:@"I-85"]) {
[exits setText:@"Exit 1 Exit 2"]
}
}
@end

How can the if statement be made to work? I get errors with it, saying its looking for a semi-colon, but then adding it throws an error saying its not usable.

Also, can I make it so that Exit 1, and Exit 2 are on new lines?

xStep
Feb 19, 2012, 02:31 PM
Ok guys, I'm now on the right track!

But now, I have this:

#import "RoadItemViewController.h"

@implementation Tab2_ItemViewController

@synthesize selectedIndex, selectedItem;

- (void)viewDidLoad
{
[super viewDidLoad];

[outputLabel setText:selectedItem];
[outputImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", selectedIndex]]];
if ([[outputLabel] isEqualToString:@"I-85"]) {
[exits setText:@"Exit 1 Exit 2"]
}
}
@end

How can the if statement be made to work? I get errors with it, saying its looking for a semi-colon, but then adding it throws an error saying its not usable.

Also, can I make it so that Exit 1, and Exit 2 are on new lines?

Hint, outputLabel, assuming a UILabel, is not a NSString, but something you set in it is a NSString and isEqualToString: is a NSString method.

I'm not sure what you want with the exit strings. Do you want new line characters after each one for printing somewhere?

ArtOfWarfare
Feb 19, 2012, 02:42 PM
A newline character is \n

However, whatever you're putting the string into needs to be able to support multiple lines:

UILabel - By default only support one line. They have a property that can be set to change how many lines they can support. I believe setting the property to 0 will allow them to support any amount of lines.

UITextField - Can only support one line.

UITextView - Can display any number of lines of text.

UIWebView - Can display any number of lines of text (although setting them up to display text isn't as easy as the other objects described above - the advantage of using UIWebViews is the text can be formatted with multiple styles by using HTML tags.)

dejo
Feb 19, 2012, 02:56 PM
I get errors with it, saying its looking for a semi-colon, but then adding it throws an error saying its not usable.

Your code shows some problems with basic Objective-C programming. What have you done to learn the fundamentals?

bkribbs
Feb 19, 2012, 03:24 PM
Hint, outputLabel, assuming a UILabel, is not a NSString, but something you set in it is a NSString and isEqualToString: is a NSString method.

I'm not sure what you want with the exit strings. Do you want new line characters after each one for printing somewhere?

That part of the code functions fine. Its the if part that I can't get to work.

A newline character is \n

However, whatever you're putting the string into needs to be able to support multiple lines:

UILabel - By default only support one line. They have a property that can be set to change how many lines they can support. I believe setting the property to 0 will allow them to support any amount of lines.

UITextField - Can only support one line.

UITextView - Can display any number of lines of text.

UIWebView - Can display any number of lines of text (although setting them up to display text isn't as easy as the other objects described above - the advantage of using UIWebViews is the text can be formatted with multiple styles by using HTML tags.)

Thanks that's all very useful!

Your code shows some problems with basic Objective-C programming. What have you done to learn the fundamentals?

I don't know them all that well. I have read online some of the basics and am still working on it. Not to mention for now this is all I want to get done; before starting on bigger projects I will really learn. But where is the flaw in the if statement?

ArtOfWarfare
Feb 19, 2012, 05:17 PM
Regarding your if, I have two questions for you to consider:

1 - What type of object is outputLabel?

2 - What type of object responds to isEqualToString: ?

xStep
Feb 19, 2012, 05:42 PM
That part of the code functions fine. Its the if part that I can't get to work.

I and ArtOfWarfare have given you the same hint. Figure it out.

bkribbs
Feb 19, 2012, 06:30 PM
I and ArtOfWarfare have given you the same hint. Figure it out.

Just from previous forums that I have been on, from a noob to a knowledgable person, to a mod, saying "figure it out" isn't the best approach. I'm researching and asking for pointers, which as you can see, ArtOfWarfare (and several others) have been kind enough to provide.

Regarding your if, I have two questions for you to consider:

1 - What type of object is outputLabel?

2 - What type of object responds to isEqualToString: ?

Thanks

xStep
Feb 19, 2012, 08:10 PM
Just from previous forums that I have been on, from a noob to a knowledgable person, to a mod, saying "figure it out" isn't the best approach. I'm researching and asking for pointers, which as you can see, ArtOfWarfare (and several others) have been kind enough to provide.



Thanks

Your problem with the if statement is simple enough that the hints ArtOfWarfare and I gave were the right way to respond to the question. Whether you're a noob or an expert, part of learning requires digging in. Our responses are designed to lead you to an answer.

Read the documentation for what I'm still assuming is a UILabel and look at what type the text property is. Learn how to access that property in outputLabel and use that in your if statement.

People here tend not to give direct answers for easy programming stuff like this. They'd rather lead you to the answer so that you can learn how to find the answer on your own now and especially in the future.