Transering Cell information, from 1 UITableView to another

Discussion in 'iOS Programming' started by mraheel, May 10, 2009.

  1. mraheel macrumors regular

    Apr 18, 2009
    hi guys. Got this question.
    I wish to transfer data from one view to another located in different xibs. Example. touch cells from apple site. I want to fill one uitableview dynamically with cells that i selected from another uitableview which was populated from sql.. Both tables should be independent of each other. .like i said.. Different view controllers,

    Whats the best possible solution? Any suggestions or perhaps even a sample would be much much appreciated
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Model. View. Controller.

    You described the view and the controllers. Make an appropriate data model. Pass it to your second view controller and then let the table controlled by the second view controller display that model.

    The model can be whatever you like. Something like an array of dictionaries with the data might work. Depends on the kind of data.
  3. mraheel thread starter macrumors regular

    Apr 18, 2009
    hey, thx for ur reply..
    I have a little understanding of objective c. What im doing write now is just send the cell details from that tablev to another And reload it during didselectrow.. Im guessing dis isn't the right practise.. If you can explain in a little more layman terms? Maybe... And one more thing.. Is it a good practise to keep a tablev completely in edit mode..?
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Layman's terms?

    This is often described as a Master-Detail UI. There is a list of 'things' in the Master view. Tapping one of them goes to a Detail view that displays one of them in more detail. It might be possible to edit the detail object or not, depending on the app.

    What you describe might be a correct solution, depending on what you mean by 'send the cell details.' If you take the current info that you want to be displayed in the detail view and build a new view controller and pass to it the current info, that's a legitimate style. I often build a custom init method that takes an object that is to be displayed in the detail view. So if I have a Person object, I display some or all the Persons in the Master view and then when you tap one of them I create a new view controller like

    PersonController = [[PersonController alloc] initWithPerson:person];
    // push the controller etc.
    So the single person object is passed to the PersonController init method and it only knows about that single object.
  5. mraheel thread starter macrumors regular

    Apr 18, 2009
    thanks for your answer man,

    By cell details i meant the cell.text and other tags that i associated it with.

    The difficulty im having is to do a good efficient cell "copy and add" to another tableview.
    Its more of a sequence of events issue i think that I didnt get.

    Its not the detailView i'm looking for actually.

    I'm sure your aware of TouchCells example, All I want is basically..
    - Tap the Cell on tableView1
    - Cell gets highlighted and "checked"
    - While that Cell is added to another TableView2 in another view dynamically in the background.
    -Theres no change of view involved here. Only passing of data and its addition in the empty tableview in the background.

    Please forgive me if any of this isnt making sense. I'm still getting used to the terminology. It took me a while to figure out what "methods" refer to..
  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    It still comes down to MVC. In your case you need to pass a model object from one view controller to another.

    There are three basic ways to do this. Pass it directly via some method. Using the initWithPerson method that I mentioned is one example. Having a simple setPerson on one view controller lets the second view controller pass the data. Obviously view controller one needs to know about view controller two for this to work.

    Second, you can use a singleton object to hold the data. The app delegate is a singleton and is commonly used for this purpose but you could write your own singleton object if that's appropriate. If you add a setCurrentPerson/currentPerson method to the app delegate your first view controller can set the current selected person when it changes. The second view controller then needs to always get the currentPerson from the app delegate when the view controller becomes visible and then reloadData on the table. In this case the view controllers don't need to know about each other, they just need to know about the app delegate.

    Third is to use NSNotificationCenter. Your first view controller posts a notification that the current row has changed and passes the model object with the info from the current row in the notification. The second view controller subscribes to the notification and updates the table with the new data when it arrives. Using notification allows one view controller to know about a process without having to know about the object that controls that process, which can simplify things in many cases.
  7. mraheel thread starter macrumors regular

    Apr 18, 2009
    THIS is exactly what I wanted. Thanks alot man. I'm gonna try and implement all the ways u mentioned and see, thats how i'll learn anyway.

    thanks again man!

    now gotta google NSNotifi..
  8. mraheel thread starter macrumors regular

    Apr 18, 2009
    aa Hi again!

    So, i read through the whole NSNotificationCenter, it really opens up many possibilities of how an app communicates. While doing so, I realised, that this is good for sending Events which are preset.
    For example, make the textRed on another view. I could jus send the command "RedSet" and there it is.

    Is there a way I could send a NSObject ?

    this is the selector that handles the notification sent.

    - (void) trackNotifications: (NSNotification *) notification
    id nname = [notification name];
    if([nname isEqual:mad:"addCell"])
    // I need to get the object and add it to table view and reload it

    and clues here would be great :) thanks
  9. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Sure, why not. When you post a notification you can specify the object that sent the notification and a userInfo dictionary. Put whatever you like into that dictionary. The method receiving the notification can get the userInfo dictionary from the notification and get out whatever you've put into it.

    You would normally set a specific selector for each kind of notification that a view controller wants to receive so each method receives only a single kind of notification.
  10. mraheel thread starter macrumors regular

    Apr 18, 2009
    Finallly..... i succeeded in doing something useful!

    following up on this, since my testapp is based on sqlitebooks source.

    I created a customcell for the table view, added a (touchedcell:)method via (addTarge:)

    In that touchedcell method, i created an instance of Book.h NSObject
    read the currect selected text from that cell into the book, had it sent via NotificationCenter.

    Meanwhile, i needed a way to send the primarykey to the other table too, not just the string displayed . Book.h as primaryKey as readonly, I removed it, Created a new initialising method that only returns the assigned values.
    And had it sent.

    Now these things did work! I'm not sure how clean it is though!

    anyone who's like me and needs this HowTo thing, i'll post the entire code.. ( Dont condemn me for the neatness.. ) ;)

Share This Page