Help with connecting UITableView to external TableViewDelegate/DataSource classes

Discussion in 'iOS Programming' started by Mac_Max, Apr 16, 2012.

  1. Mac_Max macrumors 6502

    Joined:
    Mar 8, 2004
    #1
    Hi all,

    So I'm taking my first stab at writing an iPad app for a client. It's a port of a fairly boring line of business app that does some basic math, data storage, and provides a map of deliveries. Did I mention that the app itself is boring :D. I told my client up front that this is my first stab at this and he was fine with that (and it helps that I massively discounted my rate ;)).

    What isn't boring is trying to be extremely picky about how it works. I've figured out just fine how to a UITableView populated with data and doing multiple selection seems to be straightforward enough that I'm not too worried about it. Not that it's particularly pertinent (or at least I don't think it is), my app is a Tab Based storyboard app that targets the latest version of the iOS SDK.

    What I am having trouble with is putting two UITableViews on the same view. The UITableViews should have the same functionality so I could reuse the same controller code but with different data. The problem is I've tried setting the datasource of the table to another class implementing the proper UITableViewDataSource Protocol but the data itself never shows up. In all cases I've made sure to implement the required methods that the protocol specifies.

    More specifically here's what I've tried:

    1: I've tried calling setDataSource on the table and refreshing the data. I just noticed there's a reloadData method that I haven't tried. I also tried code like:

    Code:
    myTVController* Controller = [[myTVController alloc] init]; //
    UITableView* TempTV = [[UITableView alloc] init];
    [TempTV setDelegate:Controller];
    [TempTV setDataSource:Controller];
    TView1 = Controller;
    
    But I'm guessing that I'm missing something. Perhaps reloadData?

    2: I tried modifying the cells themselves by trying to do an (in psudocode)
    Code:
    if(sender == TView1IBOutlet){ CellData =  DSource1[row] } else { CellData =  DSource2[row]  } 
    
    ...but that doesn't work because I'm comparing the address of a TableView to the address of a TableView IBOutlet (or at least that's what I understand from watching the debugger local watch window). My ObjectiveC/Cocoa background is not as strong as I'd like it to be so if there's something I'm missing that will make this work, I'd be hunky dory doing it this way.

    3: I tried creating a separate UITableViewController class to handle the DataSource/View logic and added it as an IBOutlet in my main view. The problem is that for the life of me I can't seem to figure out how to connect them and get something meaningful from it.

    ---

    I've googled around for some guides (Dr Dobbs, Cocoa With Love, StackOverflow etc) but I haven't had one that has helped me figure out anything more than just setting my view to a UITableViewController and being limited to just one TableView per page. I could in theory change the program flow so that it functions that way, but for the sake of learning how to do it the right way I'd like to avoid that.

    Any help is appreciated.

    Thanks,
    Max
     
  2. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    Assuming you have 2 tableviews subviews (tableview1 and tableview2) in a view that is is part of a view controller then in the required datasource methods determine which view is which and take the correct action, for example:

    in the numberOfRowsInSection: method do something like this (assumes data for tables is in arrays tableviewData1 and tableviewData2):

    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    if(tableView == tableView1)
    return [tableview1Data count];
    else
    return [tableviewData2 count];
    }
    
    
    
    Likewise you would do something similar for the other required data source methods.
     
  3. Mac_Max thread starter macrumors 6502

    Joined:
    Mar 8, 2004
    #3
    I tried something like this but tableView and tableView1 variables had different addresses apparently. Is there a chance that I declared the referencing variables in the header incorrectly? I believe I did something like this (but I'd have to check when I get home):

    Code:
    
    @interface myTableViewController : UITableViewController
    {
         UITableView *tableView1;
    }
    @property (nonatomic, retain) IBOutlet UITableView *tableView1;
    
    
    but off the top of my head there's a chance I might have done this instead:

    Code:
    
    @interface myTableViewController : UITableViewController
    {
        IBOutlet UITableView *tableView1;
    }
    @property (nonatomic, retain)  UITableView *tableView1;
    
    
    Would either of these declarations cause the issue I saw (that the IBOutlet reference and the tableView pointer passed to the function had different addresses)?
     
  4. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #4
    I think your problem is you cannot have two table view inside a tableviewcontroller, add the two table views to a view controller
     
  5. Mac_Max thread starter macrumors 6502

    Joined:
    Mar 8, 2004
    #5
    I got it working. I had some old code that was realloc/initing the first IBOutlet and messing up my if statement. Thanks for taking time to help me out cMacSW :).
     

Share This Page