Reputation: 15
Okay, so I'm writing an iPhone application, and to load the data into the grouped table views, I created a data model:
DataModel.m
#import "GeometryDataModel.h"
@implementation GeometryDataModel
@synthesize titleGeo =_titleGeo;
-(id)initWithTitleGeo:(NSString *)titleGeo
{
if ((self = [super init])) {
self.titleGeo = titleGeo;
}
return self;
}@end
Then I continued to write a data table...
DataTable.m
#import "GeometryTableData.h"
#import "GeometryDataModel.h"
@implementation GeometryTableData
@synthesize dataGeo = _dataGeo;
@synthesize thumbImageGeo = _thumbImageGeo;
- (id)initWithTitleGeo:(NSString *)titleGeo thumbImageGeo:(UIImage *)thumbImageGeo {
if ((self = [super init]))
{
self.dataGeo = [[GeometryDataModel alloc] initWithTitleGeo:titleGeo];
self.thumbImageGeo = _thumbImageGeo;
}
return self;
}
@end
then I created a view controller to set it all up
ViewController.m
#import "GeometryViewController.h"
#import "GeometryTableData.h"
#import "GeometryDataModel.h"
@interface GeometryViewController ()
@end
@implementation GeometryViewController
@synthesize geoCalculatorList = _geoCalculatorList;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _geoCalculatorList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *geoCell = [tableView dequeueReusableCellWithIdentifier:@"Geometry"];
GeometryTableData *geoTableStuff = [self.geoCalculatorList objectAtIndex:indexPath.row];
geoCell.textLabel.text = geoTableStuff.dataGeo.titleGeo;
geoCell.imageView.image = geoTableStuff.thumbImageGeo;
return geoCell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
And then finally, in the AppDelegate.m file I implement it to the table:
sectionOne = [[TableData alloc] initWithTitle:@"A cell" thumbImage:[UIImage
imageNamed:@"none.png"]];
sectionTwo = [[TableData alloc] initWithTitle:@"Another separated cell" thumbImage:[UIImage
imageNamed:@"none.png"]];
NSMutableArray *TableData = [NSMutableArray arrayWithObjects:sectionOne,sectionTwo, nil];
UITabBarController * tabController = (UITabBarController *) self.window.rootViewController;
ViewController * algebraController = [tabController.viewControllers objectAtIndex:0];
Controller.calculatorList = TableData;
My question is, how would I create sections in the grouped table view, say to put "sectionOne" and "sectionTwo" into different sections on the table using my current system?
Thanks!!!
Upvotes: 0
Views: 1430
Reputation: 104092
I'm not sure what you mean by putting "sectionOne" and "sectionTwo" in different sections. Do you want them to be the section views? In general, you put data in sections by having an array that's more complicated than a simple array. It could take several forms, an array of arrays is probably the simplest. If your data is set up that way, then you pass theData.count in numberOfSectionsInTableView and [[theData indexAtObject:section] count] in numberOfRowsInSection. In cellForRowAtIndexPath you would use something like this:
cell.textLabel.text = [[theData objectAtIndex:indexPath.section] objectAtIndexPath:indexPath.row];
You can use the new array syntax to shorten that to :
cell.textLabel.text = theData[indexPath.section][indexPath.row];
Upvotes: 1