2

我正在开发一个显示公司数据的表格视图。我想将数据分成 3 个部分,使其看起来更有条理。

关于一家公司的数据是从 mysql 数据库中检索的,我在一个数组中接收它,如下所示:

{
    companyAdress = "the street 9";
    companyCity = "city";
    companyFacebook = "facebook.com/companyname";
    companyName = "name";
    companyPhoneNumber = "0123 456 789";
    companyTwitter = "www.twitter.com/companyname";
    companyWebsite = "www.companyname.com";
    companyZip = "0000 AA";
    imageNumber = "3067913";
}

我想要第一部分中的companyNameand ,第二部分中的, and ,以及第三部分中的所有剩余变量。imageNumbercompanyAdresscompanyZipcompanyCity

我不知道如何正确执行此操作,并且在 SO 或我知道的任何其他网站上都没有找到有用的答案/解决方案。

我该怎么做?任何帮助、示例代码和/或教程将不胜感激,在此先感谢您!

4

2 回答 2

1

一种方法是在将数据接收到二维数组时将其分离。companyName所以数组的第一个条目将是一个包含and的数组imageNumber,依此类推。

使用此实现,numberOfSectionsInTableView将简单地返回myArray.countnumberOfRowsInSection返回myArray[section].count

要从那里访问适当的值,您可以执行类似的操作((NSMutableArray*)myArray[indexpath.section])[indexpath.row]

于 2013-05-17T15:12:36.977 回答
1

您必须使用 NSDictionary 项的数组,然后才能获取部分和表行的信息。为每种记录类型添加一个键。

这是一个示例项目,用于解释 NSArray 和 NSDictonary 的使用,希望对您有所帮助。

您可以从这里http://www.germinara.it/download/FGTestTableView.zip下载 xcode 项目,这是示例http://www.germinara.it/download/FGtesttableview.png的结果

#import <UIKit/UIKit.h>

@interface FGViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {

    NSMutableArray* records;
}

@property(nonatomic,strong) IBOutlet UITableView *tblRecordsList;

-(void) buildDataSource; //Build the datasource for the tableview

@end


#import "FGViewController.h"

@interface FGViewController ()

@end

@implementation FGViewController

@synthesize tblRecordsList;

- (void)viewDidLoad
{
    [super viewDidLoad];

    records = [[NSMutableArray alloc] init];

    //Load data into array used as datasource
    [self buildDataSource];

    self.tblRecordsList.dataSource=self;
    self.tblRecordsList.delegate=self;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//Load sample data
-(void) buildDataSource{
    NSMutableDictionary* dict= nil;
    [records removeAllObjects];

    //Fill data source with your data 

    //Data to put on first section
    dict=[NSMutableDictionary dictionaryWithCapacity:0];
    [dict setObject:@"0" forKey:@"idsection"];
    [dict setObject:@"company1" forKey:@"companyName"];
    [dict setObject:@"picture1" forKey:@"imageNumber"];
    [records addObject:dict]; //Add items to array


    //Data to put on second section
    dict=[NSMutableDictionary dictionaryWithCapacity:0];
    [dict setObject:@"1" forKey:@"idsection"];
    [dict setObject:@"address1" forKey:@"companyAdress"];
    [dict setObject:@"zip1" forKey:@"companyZip"];
    [dict setObject:@"city1" forKey:@"companyCity"];
    [records addObject:dict]; //Add items to array


    //Data to put on other section
    dict=[NSMutableDictionary dictionaryWithCapacity:0];
    [dict setObject:@"2" forKey:@"idsection"];
    [dict setObject:@"facebook1" forKey:@"companyFacebook"];
    [dict setObject:@"phone1" forKey:@"companyPhoneNumber"];
    [dict setObject:@"twitter1" forKey:@"companyTwitter"];
    [dict setObject:@"website1" forKey:@"companyWebsite"];
    [records addObject:dict]; //Add items to array

}

//Get Dictionary using section key (idsection)
-(NSDictionary *) dictionaryForSection:(NSInteger) section{
    for (NSDictionary *dict in records){
        if(section == [[dict valueForKey:@"idsection"] intValue]){
            return dict;
        }
    }
    return nil;
}

//Table View Delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell =nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"myCellReuseID"];

    NSDictionary * dict = [self dictionaryForSection:indexPath.section]; //Get request dictionary info

    //Process data for first section
    if(indexPath.section == 0){
     if(indexPath.row == 0)
      cell.textLabel.text=[dict valueForKey:@"companyName"];
     if(indexPath.row == 1)
      cell.textLabel.text=[dict valueForKey:@"imageNumber"];
    }

    //Process data for second section
    if(indexPath.section == 1){
        if(indexPath.row == 0)
            cell.textLabel.text=[dict valueForKey:@"companyAdress"];
        if(indexPath.row == 1)
            cell.textLabel.text=[dict valueForKey:@"companyZip"];
        if(indexPath.row == 2)
            cell.textLabel.text=[dict valueForKey:@"companyCity"];
    }

    //Process data for other section
    if(indexPath.section == 2){
        if(indexPath.row == 0)
            cell.textLabel.text=[dict valueForKey:@"companyFacebook"];
        if(indexPath.row == 1)
            cell.textLabel.text=[dict valueForKey:@"companyPhoneNumber"];
        if(indexPath.row == 2)
            cell.textLabel.text=[dict valueForKey:@"companyTwitter"];
        if(indexPath.row == 3)
            cell.textLabel.text=[dict valueForKey:@"companyWebsite"];
    }


    return cell;
}

//Number of sections (first,second and other => 3)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section
{
    NSString * sectionTitle =@"";
    switch (section) {
        case 0:
            sectionTitle = @"title first section";
            break;
        case 1:
            sectionTitle = @"title second section";
            break;
        case 2:
            sectionTitle = @"title other section";
            break;
        default:
            break;
    }
    return sectionTitle;
}


//Count number of record for sections
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int nRecords=0;
    int idSection =0;
    //Count number of items for specified section
    for (NSDictionary *dict in records){
      idSection = [[dict valueForKey:@"idsection"] intValue];
        if(section == idSection){
            nRecords = [[dict allKeys] count] -1 ; //All dictionary Keys - 1 (the first key "idsection")
        }
    }
    return nRecords;
}



@end
于 2013-05-17T15:59:04.403 回答