如何使用一个数组填充更多tableview部分

时间:2013-05-17 15:03:10

标签: iphone objective-c xcode4.5 uitableview

我正在制作一个显示公司数据的tableview。我想将数据划分为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";
}

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

我不知道如何正确地做到这一点,我还没有在SO或我认识的任何其他网站上找到有用的答案/解决方案。

我该怎么做?任何帮助,示例代码和/或教程将非常感谢,提前谢谢!

2 个答案:

答案 0 :(得分:1)

一种方法是在将数据接收到二维数组时将数据分开。因此,数组的第一个条目是一个包含companyNameimageNumber的数组,依此类推。

通过此实施,numberOfSectionsInTableView只会返回myArray.countnumberOfRowsInSection将返回myArray[section].count

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

之类的操作

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