iPhone SDK:如何在UITableView上创建节

时间:2022-08-25 07:52:04

I have this data that I need to put inside my UITableView but I'm getting confused on how to properly implement it. I cannot properly separate the values to segregate the Boys data to the Girls data.

我有这个数据需要放在我的UITableView中,但我对如何正确实现它感到困惑。我无法正确分隔值以将Boys数据与Girls数据分开。

{
"QUERY": {
    "COLUMNS": [
        "NAME",
        "GENDER"
    ],
    "DATA": [
    [
        "Anne",
        "Girl"
    ],
    [
        "Alex",
        "Boy"
    ],
    [
        "Vince",
        "Boy"
    ],
    [
        "Jack",
        "Boy"
    ],
    [
        "Shiela",
        "Girl"
    ],
    [
        "Stacy",
        "Girl"
    ]
  ]
},
"TOTALROWCOUNT": 6
}

I have this codes:

我有这个代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [genderArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [genderArray objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [namesArray count];
}

namesArray has all the values returned by NAME while genderArray has all the values of GENDER. I'm getting confused.

namesArray具有NAME返回的所有值,而genderArray具有GENDER的所有值。我很困惑。

1 个解决方案

#1


6  

AS you are getting confused, break your data into pieces. You want two arrays, one per section. So you want one array of boys names, and another array of girls names.

当你感到困惑时,将你的数据分成几部分。你想要两个数组,每个部分一个。所以你想要一个男孩名字阵列和另一个女孩名字。

You can obtain this by iterating through the embedded DATA array.

您可以通过迭代嵌入的DATA数组来获得此结果。

Turn your data into an NSDictionary object. Your data looks like JSON so...

将您的数据转换为NSDictionary对象。您的数据看起来像JSON所以......

    NSDictionary* myDict =  [NSJSONSerialization JSONObjectWithData:myJsonData 
                                                            options:0 error:&error];

Extract the data...

提取数据......

    NSArray* dataArray = [myDict objectForKey:@"DATA"];

Iterate...

    NSMutableArray* boys = [[NSMutableArray alloc] init];
    NSMutableArray* girls = [[NSMutableArray alloc] init];
    for (id person in dataArray) {
         if ([[person objectAtIndex:1] isEqualToString:@"Girl"])
              [girls addObject:[person objectAtIndex:0]];
         else [boys  addObject:[person objectAtIndex:0]]; 
     }

Now you have two arrays, one for each of your table sections. Make an array of sections, and put these arrays into it:

现在您有两个数组,每个数组对应一个表部分。创建一个部分数组,并将这些数组放入其中:

    NSArray* sections = [NSArray arrayWithObjects:boys,girls,nil];

Make a separate array for your section headers:

为节标题创建一个单独的数组:

    NSArray* headers = [NSArray arrayWithObjects:@"Boys",@"Girls",nil];

Now your data source methods look like this:

现在您的数据源方法如下所示:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [sections count];
    }

    - (NSString *)tableView:(UITableView *)tableView 
    titleForHeaderInSection:(NSInteger)section
    {
        return [headers objectAtIndex:section];
    }

    - (NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section
    {
        return [[sections objectAtIndex:section] count];
    }

and finally

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

    ...

       cell.textLabel.text = (NSString*)[[self.sections objectAtIndex:indexPath.section]   
                                                        objectAtIndex:indexPath.row];

#1


6  

AS you are getting confused, break your data into pieces. You want two arrays, one per section. So you want one array of boys names, and another array of girls names.

当你感到困惑时,将你的数据分成几部分。你想要两个数组,每个部分一个。所以你想要一个男孩名字阵列和另一个女孩名字。

You can obtain this by iterating through the embedded DATA array.

您可以通过迭代嵌入的DATA数组来获得此结果。

Turn your data into an NSDictionary object. Your data looks like JSON so...

将您的数据转换为NSDictionary对象。您的数据看起来像JSON所以......

    NSDictionary* myDict =  [NSJSONSerialization JSONObjectWithData:myJsonData 
                                                            options:0 error:&error];

Extract the data...

提取数据......

    NSArray* dataArray = [myDict objectForKey:@"DATA"];

Iterate...

    NSMutableArray* boys = [[NSMutableArray alloc] init];
    NSMutableArray* girls = [[NSMutableArray alloc] init];
    for (id person in dataArray) {
         if ([[person objectAtIndex:1] isEqualToString:@"Girl"])
              [girls addObject:[person objectAtIndex:0]];
         else [boys  addObject:[person objectAtIndex:0]]; 
     }

Now you have two arrays, one for each of your table sections. Make an array of sections, and put these arrays into it:

现在您有两个数组,每个数组对应一个表部分。创建一个部分数组,并将这些数组放入其中:

    NSArray* sections = [NSArray arrayWithObjects:boys,girls,nil];

Make a separate array for your section headers:

为节标题创建一个单独的数组:

    NSArray* headers = [NSArray arrayWithObjects:@"Boys",@"Girls",nil];

Now your data source methods look like this:

现在您的数据源方法如下所示:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [sections count];
    }

    - (NSString *)tableView:(UITableView *)tableView 
    titleForHeaderInSection:(NSInteger)section
    {
        return [headers objectAtIndex:section];
    }

    - (NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section
    {
        return [[sections objectAtIndex:section] count];
    }

and finally

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

    ...

       cell.textLabel.text = (NSString*)[[self.sections objectAtIndex:indexPath.section]   
                                                        objectAtIndex:indexPath.row];