简体   繁体   中英

to expand or collapse rows at different sections in iOS 7

I'm working on an application where i need to expand collapse rows in different sections. Initially I tried expanding collapsing rows with a single section. Now I want the same to happen in multiple sections.

here is my code

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
       //Return the number of sections.
       return [mysection count];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
          NSArray *rate=[mysection objectAtIndex:section];

          return [myrow count];

    }

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

    if (selectedSection==indexPath.section) {


    if(selectedIndex==indexPath.row){
        selectedSection=-1;
        selectedIndex=-1;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        return;

    }
    //user taps on different row
    if(selectedIndex!=indexPath.row){

        NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];

        selectedSection=indexPath.section;

        selectedIndex=indexPath.row;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

    }

    selectedIndex=indexPath.row;

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    return;
}

else if(selectedSection!=indexPath.section){


    selectedSection=indexPath.section;


    [tableView reloadSections:[NSIndexSet indexSetWithIndex:selectedSection] withRowAnimation:UITableViewRowAnimationFade];

    if(selectedIndex==indexPath.row){
        selectedSection=-1;
        selectedIndex=-1;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        return;

    }
    //user taps on different row
    if(selectedIndex!=indexPath.row){

        NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];

        selectedSection=indexPath.section;

        selectedIndex=indexPath.row;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

    }

    selectedIndex=indexPath.row;

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

selectedSection=indexPath.section;

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

NSLog(@"%d",selectedSection);
    }

only my rows should be expanded not my sections

Please provide with some possible solutions.

thanks in advance

my table view has multiple sections and my section has many rows

tapping on row in that section should expand

In didSelectRowAtIndexPath i get the index path to a variable that can be accessed throughout the coding as below...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedCellIndexPath = indexPath;
[tableView reloadData];
}

in Heightforrowatindexpath

if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] ==NSOrderedSame)
    {
        return height;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM