Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bkribbs

macrumors 65816
Original poster
Jan 15, 2012
1,178
0
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

macrumors G3
Nov 26, 2007
9,558
6,058
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

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
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 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. I haven't looked at it.
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 65816
Original poster
Jan 15, 2012
1,178
0
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors 65816
Original poster
Jan 15, 2012
1,178
0
Ok guys, I'm now on the right track!

But now, I have this:

Code:
#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

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
Ok guys, I'm now on the right track!

But now, I have this:

Code:
#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

macrumors G3
Nov 26, 2007
9,558
6,058
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.)
 

bkribbs

macrumors 65816
Original poster
Jan 15, 2012
1,178
0
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

macrumors G3
Nov 26, 2007
9,558
6,058
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: ?
 

bkribbs

macrumors 65816
Original poster
Jan 15, 2012
1,178
0
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

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.