Skip to content

Instantly share code, notes, and snippets.

@agiletalk
Created April 21, 2011 05:24
Show Gist options
  • Save agiletalk/933763 to your computer and use it in GitHub Desktop.
Save agiletalk/933763 to your computer and use it in GitHub Desktop.
Two UITableView in One UIViewController
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows;
if(tableView == aTable) rows = [aList count];
if(tableView == bTable) rows = [bList count];
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TwoTablesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if(tableView == aTable)
cell.textLabel.text = [aList objectAtIndex:indexPath.row];
if(tableView == bTable)
cell.textLabel.text = [bList objectAtIndex:indexPath.row];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *__title, *__message;
if(tableView == aTable) {
__title = @"aTable";
__message = [aList objectAtIndex:indexPath.row];
}
if(tableView == bTable) {
__title = @"bTable";
__message = [bList objectAtIndex:indexPath.row];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:__title message:__message delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
[alert show];
[alert release];
}
@tobitech
Copy link

tobitech commented Feb 5, 2016

What if the TableViews are to use two different custom UITableViewCell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment