0

好的,所以我正在编写一个 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”放入表的不同部分?

谢谢!!!

4

1 回答 1

1

我不确定将“sectionOne”和“sectionTwo”放在不同的部分是什么意思。您希望它们成为剖面视图吗?通常,您可以通过一个比简单数组更复杂的数组来将数据放在部分中。它可以采用多种形式,数组数组可能是最简单的。如果您的数据是这样设置的,那么您在 numberOfSectionsInTableView 中传递 theData.count,在 numberOfRowsInSection 中传递 [[theData indexAtObject:section] count]。在 cellForRowAtIndexPath 中,您将使用如下内容:

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

您可以使用新的数组语法将其缩短为:

cell.textLabel.text = theData[indexPath.section][indexPath.row];
于 2013-01-24T03:52:38.343 回答