如何在indexPathForSelectedRow中包含节信息

时间:2014-11-24 21:58:53

标签: xcode uitableview segue sections

我有一个包含部分的表,由一个由四个数组组成的数组填充。如何修改prepare for segue方法中的indexPathForSelectedRow语句以包含节信息?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([[segue identifier]isEqualToString:@"plantSaveSegue"])
    {
        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        DetailViewController *grassType = [segue destinationViewController];

        //grassType.selectedPlant = [grassArray objectAtIndex:selectedRowIndex.row];
        grassType.selectedPlant = [plantArray objectAtIndex:selectedRowIndex.row];
    }
}

这适用于一个部分,但我该如何处理四个部分?

2 个答案:

答案 0 :(得分:0)

在.h文件中声明2个NSIntegers

NSInteger selectedSection;
NSInteger selectedRow;

现在在viewDidLoad()中,将这些初始化为" -1"

selectedSection = -1;
selectedRow = -1;

现在在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


selectedSection = indexPath.section;
selectedRow = indexPath.row;

//Now performSegue here
}

之后

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([[segue identifier]isEqualToString:@"plantSaveSegue"])
    {
        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        DetailViewController *grassType = [segue destinationViewController];

        if(selectedSection == 0){
             grassType.selectedPlant = [firstArray objectAtIndex:selectedRow];}
        else if(selectedSection == 1){
             grassType.selectedPlant = [secondArray objectAtIndex:selectedRow];}
        else if(selectedSection == 2){
             grassType.selectedPlant = [thirdArray objectAtIndex:selectedRow];}
        else if(selectedSection == 3{
             grassType.selectedPlant = [fourthArray objectAtIndex:selectedRow];}

    }
}

希望这有助于

答案 1 :(得分:0)

感谢所有的帮助和指导。答案很简单,输入表中相应项目的完整和正确的路径。见下文:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:@"plantSaveSegue"])
{
    NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
    DetailViewController *grassType = [segue destinationViewController];
    grassType.selectedPlant = [[plantArray objectAtIndex:selectedRowIndex.section]objectAtIndex:selectedRowIndex.row];
}
}