2013-01-24 28 views
0

好了,我正在寫一個iPhone應用程序,以及將數據加載到分組表視圖,我創建了一個數據模型:在iOS應用上添加組表視圖部分?

DataModel.m 

#import "GeometryDataModel.h" 

@implementation GeometryDataModel 
@synthesize titleGeo =_titleGeo; 

-(id)initWithTitleGeo:(NSString *)titleGeo 
{ 
    if ((self = [super init])) { 
     self.titleGeo = titleGeo; 
    } 
    return self; 
}@end 

然後我繼續寫數據表...

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 

然後我創建了一個視圖控制器來設置這一切

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 

然後終於,在AppDelegate.m文件我把它落實到表:

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; 

我的問題是,我將如何創建分組表視圖部分,說把「sectionOne」和「sectionTwo」到桌子上不同的部分使用我當前的系統?

謝謝!

+0

你有一個類資料表和變量表資料 - 可能不是最好的主意。您的變量和屬性應遵循樣式指南並以小寫字母開頭。那麼,sectionOne和sectionTwo是什麼類型的對象(我知道TableData,但是它的子類是什麼)? – rdelmar

+0

TableData爲對象提供標題(NSString)和圖像(UIImage)的屬性。在一天結束時,它是NSObject的一個子類。 - PS。謝謝你向我指出變量問題,總是有助於再次擁有一雙眼睛! – user1751797

回答

1

我不確定在不同部分放置「sectionOne」和「sectionTwo」是什麼意思。你想讓他們成爲部分的意見?一般來說,通過使數組比複雜數組複雜,可以將數據放入節中。它可能有幾種形式,一組數組可能是最簡單的。如果以這種方式設置數據,那麼在numberOfRowsInSection中傳遞numberOfSectionsInTableView中的Data.count和[[theData indexAtObject:section] count]。在的cellForRowAtIndexPath你可以使用這樣的事情:

cell.textLabel.text = [[theData objectAtIndex:indexPath.section] objectAtIndexPath:indexPath.row]; 

您可以使用新的數組語法縮短,爲:

cell.textLabel.text = theData[indexPath.section][indexPath.row];