views:

140

answers:

3

Hi there

I need an example or explanations of how to populate 2 table views which are on the same view. I need to understand the "cellForRowAtIndexPath" method, could someone provide me an example on how should the code be?

I mean how to identify which goes which table view?

Thanks

Below is my cellForRowAtIndexPath method:

 // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

// Configure the cell...
// Set up the cell
MyAppAppDelegate *appDelegate = (MyAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tableView == radios_tv) { //radio_tv is an IBOutleet UITableView
        sqlClass *aRadio = (sqlClass *)[appDelegate.array_radios objectAtIndex:indexPath.row];
        [cell setText:aRadio.r_name];
        return cell;
    }
    if (tableView == presets_tv) { //preset_tv is an IBOutlet UITableView


    }

}
+2  A: 

IMO the cleanest solution would be to have one controller for each tableview.

radios_tv would call it own delegate's method, while presets_tv calls it own.

edit

if you use one controller for n tableview, you will have to use if-statemenst in many places, in

  • – numberOfSectionsInTableView:
  • – tableView:numberOfRowsInSection:
  • – tableView:titleForHeaderInSection:

basically in all UITableViewDatasource-Protocol methods that you will need to implement.

So if you need to change something, you have to change it in many places.

If you use one controller class for one tableview, you won't have to check at all.

  1. write a controller class for every tableview, make it conforming to the UITableViewDatasource protocol
    • implement the protocol methods you will need. at least
      • – numberOfSectionsInTableView:,
      • – tableView:numberOfRowsInSection:,
      • – tableView:cellForRowAtIndexPath:
  2. call -setDataSource:for every tableview to an object of the right controller class

I think, it was shown in one of the WWDC 2010 videos. I am not sure, but I guess it was Session 116 - Model-View-Controller for iPhone OS.

edit

I wrote an example code: http://github.com/vikingosegundo/my-programming-examples

vikingosegundo
and how do we do this? What do you mean by controller? Sorry, I'm quite a newbie :s
okayasu
I am searching for an example right now. please stay tuned :)
vikingosegundo
thanks :D I've looked for an example too, but i've been unable to find any :(
okayasu
I will try it tomorrow at work :D thank you very much vikingosegundo :D
okayasu
sorry vikingosegundo, what do you mean by one controller for one tableview? could you provide an example please?
okayasu
I mean that u have own Controller classes for the different TableViews, as this keeps u from doing to many if-statements
vikingosegundo
see the code i posted
vikingosegundo
Hey Viking, I successfully adapted your code to my code, it helped a lot :D However I have one more question, how to make the cells selectable? Thanks in advance.
okayasu
No it's okay xD Thank you :D
okayasu
A: 

vikingosegundo, would it be better if 2 controllers are created for the TableView but the TableViews are shown on the same view? That way it wont be troublesome of have too many if-else statements in all the methods.

And Im new to stackoverflow. I dont really know how to comment on your previous answer... Unless im not suppose to..

Melvin Lai
This Is excatly what i say. Probably the methods need different implementations, so different classes are needed.
vikingosegundo
Before u can comment on any answer/question, u need to receive 50 points. See the faq.
vikingosegundo
Thank you vikingosegundo. This helps a lot :D
okayasu
ah, i see... thx vik :)
Melvin Lai
A: 

and hey vikingsegundo, now I need to delete a cell which is on my TableViewController class, how do I do this? I explain, here is my code:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //Get the object to delete from the array.
        Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
        [appDelegate removeCoffee:coffeeObj];

        //Delete the object from the table.
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

Since we put different controllers, how should we proceed for this line? Should I put the tableViewController instead of the "self"?

//Delete the object from the table.
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
okayasu
you shouldnt put new questions in Answers. Edit your original instead.
vikingosegundo
In my code i had a bug, pull it from git. U will see that my TableViewControllers now inherit from UITableviewController. Now u can post ur code in both Controllers
vikingosegundo
u could also make one of the both inherit from the other, to write less code. but it will bin back a certain amount of coupling
vikingosegundo
Okay thank you very much Vikingosegundo
okayasu
Hey vikingsegundo, how to make it inherit?
okayasu