Cells in iOS app

Discussion in 'iOS Programming' started by bkribbs, Feb 16, 2012.

  1. macrumors 65816

    Joined:
    Jan 15, 2012
    #1
    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?
     
  2. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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.)
     
  3. xStep, Feb 17, 2012
    Last edited: Feb 17, 2012

    macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
    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.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    What tutorial? Be specific.

    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.
     
  5. thread starter macrumors 65816

    Joined:
    Jan 15, 2012
    #5
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    See Part 2 of the "Beginning Storyboards in iOS 5 Part 2" tutorial for more info on using static cells with storyboards.
     
  7. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    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.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     
  9. thread starter macrumors 65816

    Joined:
    Jan 15, 2012
    #9
    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?
     
  10. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    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.
     
  11. thread starter macrumors 65816

    Joined:
    Jan 15, 2012
  12. thread starter macrumors 65816

    Joined:
    Jan 15, 2012
    #12
    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?
     
  13. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #13
    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?
     
  14. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #14
    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.)
     
  15. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Your code shows some problems with basic Objective-C programming. What have you done to learn the fundamentals?
     
  16. thread starter macrumors 65816

    Joined:
    Jan 15, 2012
    #16
    That part of the code functions fine. Its the if part that I can't get to work.

    Thanks that's all very useful!

    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?
     
  17. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #17
    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: ?
     
  18. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #18
    I and ArtOfWarfare have given you the same hint. Figure it out.
     
  19. thread starter macrumors 65816

    Joined:
    Jan 15, 2012
    #19
    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
     
  20. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #20
    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.
     

Share This Page