Ios5 尝试使用自定义分组对我的表视图进行分组(xcode 4.2 iOS 5.0)

Ios5 尝试使用自定义分组对我的表视图进行分组(xcode 4.2 iOS 5.0),ios5,xcode4.2,Ios5,Xcode4.2,我正在编写一个iPhone应用程序,我有一个表视图,它由我从txt文件提供的信息填充。我想对这些表进行分组(就像联系人的名字一样),但是我想选择哪些字段在哪个分组中 例如,对于我的应用程序,我想按类型对成分进行分组(例如乳制品、农产品等),并在那里显示相应的成分。我该怎么做?我的表视图当前有以下代码: NSString *wellBalancedIngredientFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainB

我正在编写一个iPhone应用程序,我有一个表视图,它由我从txt文件提供的信息填充。我想对这些表进行分组(就像联系人的名字一样),但是我想选择哪些字段在哪个分组中

例如,对于我的应用程序,我想按类型对成分进行分组(例如乳制品、农产品等),并在那里显示相应的成分。我该怎么做?我的表视图当前有以下代码:

NSString *wellBalancedIngredientFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Well Balanced Ingredients" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];


wellBalancedIngredients = [wellBalancedIngredientFileContents componentsSeparatedByString:@"(penguins)"];
WellbalancedComponents(作为数组之一)是一个包含所有“平衡良好的成分”的数组。我还有另外3个数组,根据用户的选择,我也用它们填充tableview

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{



    if (ingredientListChoice == 1) { 
        return [self.wellBalancedIngredients count];

    }
    else if (ingredientListChoice == 2) {
        return [self.meatIngredients count];

    }
    else if (ingredientListChoice == 3) {
        return [self.vegetarianIngredients count];

    }
    else {
        return [self.veganIngredients count];

    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    if (ingredientListChoice == 1) {
        cell.textLabel.text = [self.wellBalancedIngredients objectAtIndex:indexPath.row];
    }
    else if (ingredientListChoice == 2) {
        cell.textLabel.text = [self.meatIngredients objectAtIndex:indexPath.row];
    }
    else if (ingredientListChoice == 3) {
        cell.textLabel.text = [self.vegetarianIngredients objectAtIndex:indexPath.row];
    }
    else {
        cell.textLabel.text = [self.veganIngredients objectAtIndex:indexPath.row];
    }
    return cell; 
    // Configure the cell...

}

我如何添加到这段代码中,将分组添加到节中,然后能够自定义标题,并根据我选择的首选项填充每个节。谢谢您的帮助。

首先,您应该在表中返回所需的节数:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ 
    return numberOfSections;
}
然后,对于每个部分的标题,您可以使用:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return @"Section Title";
}

首先,应返回表中所需的节数:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ 
    return numberOfSections;
}
然后,对于每个部分的标题,您可以使用:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return @"Section Title";
}

但是,我如何选择哪些项位于哪个节下?从tableView中传递的indexPath:CellForRowatineXpath:可以知道节的索引和请求的行。但是,我如何选择哪些项位于哪个节下?从tableView中传递的indexPath:CellForRowatineXpath:可以知道节的索引和请求的行请求的行。