views:

3641

answers:

4

I need to have two UITableViews on one UIView. I can make it work with one, here is the code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [contentOne count];  // sets row count to number of items in array
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *firstValue = [[NSString alloc] initWithFormat: @"Row %i% %", indexPath.row+1 ];
    NSString *secondValue = [contentOne objectAtIndex:indexPath.row];

    NSString *cellValue = [firstValue stringByAppendingString: secondValue]; // appends two strings

    [cell.textLabel setText:cellValue];



    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

I have tried several different methods. Anyone? If I could name each UITableView a different name that should do it but it will not let me edit tableView to anything else without crashing.

+4  A: 

so you need some way to tell the two tableViews apart--you could either set the "tag" property to different values, or have a property on your view controller that points to each view

@property (nonatomic, retain) IBOutlet UITableView *tableView1;
@property (nonatomic, retain) IBOutlet UITableView *tableView2;

then hook these up to each view in interface builder...

then in your view controller methods you can do

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.tableView1) {
        return 37;
    } else if (tableView == self.tableView2) {
        return 19;
    } else {
        // shouldn't get here, use an assert to check for this if you'd like
    }
}
David Maymudes
I tried - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == self.tableOne){ return 1; } else if (tableView == self.tableTwo){ return 1; } else{ return 1; }}It gives me the error Local declaration of tableView hides instance
New to iPhone
do you have a class variable named tableView? is your class here derived from UITableViewController, or something else?
David Maymudes
I did everything inside of the UIView. I did not have a seperate controller for the UITableView. I just tried sections and it is so much easier than trying to make two seperate UITableViews. I was able to seperate my two arrays into seperate sections. Thank you for helping. I am just not getting it.
New to iPhone
separate tableViews would be good if you wanted them to be separately scrollable... if you want one list with two kinds of stuff in it then two sections in one list is the way to go. It will start to make sense eventually!
David Maymudes
+1 for tag suggestion, probably best in a switch() statement
Jamie Chapman
+6  A: 

Probably the easiest way of implementing this is to have two delegate and data source classes, one for each table view. That would reduce the number of if (tableview == tableview1) occurances in the view controller code.

Jack Cox
Could you provide an example implementation for this please?
dombesz
A: 

I am newbie ,could anyone can provide a simple example/code for this Implementation,please.. Thanks in advance...

Nandagopal T
A: 

Can you please provide more source code... or a bit further explanation?

I tried to implement this but still not getting too far:

http://stackoverflow.com/questions/3665531/dragging-uitableviewcell-between-two-uitableviews