在iOS应用上添加组表视图部分?

时间:2013-01-24 00:50:20

标签: iphone objective-c uitableview ios6

好的,我正在编写一个iPhone应用程序,并将数据加载到分组表视图中,我创建了一个数据模型:

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”放到桌子的不同部分?

感谢!!!

1 个答案:

答案 0 :(得分: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];
相关问题