【问题标题】:how to populate more tableview sections with one array如何用一个数组填充更多的 tableview 部分
【发布时间】:2013-05-17 15:03:09
【问题描述】:

我正在开发一个显示公司数据的表格视图。我想将数据分成 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 或我知道的任何其他网站上都没有找到有用的答案/解决方案。

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

【问题讨论】:

    标签: iphone objective-c xcode4.5 uitableview


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复,但MyArray[indexpath.section].count 不起作用!似乎 [indexpath.section 不能放在括号中。
    • 我通过用 if 语句替换您的方法来修复它: if (section == 0) { return 3; } if (section == 1) { return 3; } if (section == 2) { return 4; }.. 但是,这只是设置节数和每个节中的行数。我的探针实际上在每个部分的每一行中都得到了正确的值。你知道怎么做吗?
    • 我的错误。 MyArray[indexpath.section].count 应该是 MyArray[section].count。我还添加了如何访问信息。
    • myArray[section].count 不起作用。我用之前提到的方法替换了它,但现在效果很好
    【解决方案2】:

    您必须使用 NSDictionary 项的数组, 然后你会得到部分和表格行的信息。 为每种记录类型添加一个键。

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

    您可以从这里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
    

    【讨论】:

    • 感谢您的回复!由于我是初学者,我不完全明白你的意思。您能否再解释一下如何制作一系列 nsdictionary 项目,以及如何用它填充多个部分?提前谢谢!
    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2016-06-29
    • 2015-06-17
    • 1970-01-01
    • 2013-10-31
    相关资源
    最近更新 更多