Build a Drill Down TableView with arrays
Posted
by skiria
on Stack Overflow
See other posts from Stack Overflow
or by skiria
Published on 2010-05-08T17:10:33Z
Indexed on
2010/05/08
17:18 UTC
Read the original article
Hit count: 204
I'm trying to build a Drill down tableview to show the information that I have in a hierachy of arrays and classes. I have 3 classes:
class Video { nameVideo, id, description, user... }
class Topic {nameTopic, topicID, NSMutableArray videos; }
class Category {nameCategory, categoryID, NSMUtableArray topics}
And then in my app delegate I defined
//AppDelegate.h
NSMutableArray categories;
The app display the tableView with nameCategory, but then, when I select a row it shows a tableview with 0 rows. If I check, for the second view the NumberOfRowsInSection, it returns me 0. I supose that the error is that I don't build correctly the provisional array called tableDataSource, I don't know the reason why it doesn't copy the new information on this array. `
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (TSCAppDelegate *)[[UIApplication sharedApplication] delegate];
//self.title = @"Categoria";
if(CurrentLevel == 0) {
//Initialize our table data source
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource = tempArray;
[tempArray release];
self.tableDataSource = appDelegate.categories;
self.navigationItem.title = @"Categoria";
CurrentTitle = @"Categoria";
}
else {
self.navigationItem.title = CurrentTitle;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog (@"contador_ %i,", [self.tableDataSource count]);
//--> On the second view it is 0!
return [self.tableDataSource count];
}
// 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
}
// Configure the cell.
if (CurrentLevel == 0)
{
Category *aCategory = [self.tableDataSource objectAtIndex:indexPath.row];
cell.text = aCategory.nameCategory;
}
if (CurrentLevel == 1)
{
Topic *aTopic = [self.tableDataSource objectAtIndex:indexPath.row];
cell.text = aTopic.nameTopic;
}
else if ([CurrentLevel == 0]){
Topic *aTopic = [self.tableDataSource objectAtIndex:indexPath.row];
cell.text = aTopic.nameTopic;
//NSLog(@"inside!!");
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the actual Category
Category *aCategory = [self.tableDataSource objectAtIndex:indexPath.row];
//NSLog (@" CATEGORY: %@", aCategory.nameCategory);
//NSLog (@" CATEGORY: %i", aCategory.categoryID);
//NSLog (@" TOPICS OF CATEGORY: %i", [aCategory.topics count]);
//Prepare to tableview.
navegaTableViewController *nvController = [[navegaTableViewController alloc] initWithNibName:@"navegaTableView" bundle:[NSBundle mainBundle]];
//Increment the Current View
nvController.CurrentLevel += 1;
//Set the title;
nvController.CurrentTitle = @"Topic";
nvController.tableDataSource = aCategory.topics;
//Push the new table view on the stack
[self.navigationController pushViewController:nvController animated:YES];
[nvController release];
}
`
© Stack Overflow or respective owner