1

Hey everyone I made a UITableView in my app and when a cell is touched it expands the problem I am having is it does not collapse no matter what I have tried, I'm sure its something easy i just cant figure it out the only time it does collapse is when it another cell is tapped.

Here is my code:

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

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

    if (selectedCellIndexPath) {
        selected = YES;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    if(selectedCellIndexPath != nil  
       && [selectedCellIndexPath compare:indexPath] == NSOrderedSame)  
        return 150;

    return 44;
}
jonkroll
  • 15,682
  • 4
  • 50
  • 43
Sage Washabaugh
  • 297
  • 2
  • 12

4 Answers4

1

You're never changing selectedCellIndexPath to nil, so the current selected row doesn't get changed until a new one is selected. In didSelectRowAtIndexPath you should change the beginning to the following:

if (selectedCellIndexPath == indexPath)
  selectedCellIndexPath = nil;
else
  selectedCellIndexPath = indexPath;
Dima
  • 23,484
  • 6
  • 56
  • 83
0
In selectedCellIndexPath you are storing pointer of indexPath. It can change when you reload table, means indexPath object of same cell can be different when you select it second time.
It's safer if you store indexPath.section & indexPath.row 



      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {    
               if(_section == indexPath.section && _row == indexPath.row)
               {
                  _section = -1;
                  _row = -1
               }
               else
               {
                  _section = indexPath.section;
                  _row = indexPath.row;
               }   
               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

                if (selectedCellIndexPath) {//change it as per your requirement
                    selected = YES;
                }
         }

      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {        
               if(_section == indexPath.section && _row == indexPath.row)  
                   return 150;

               return 44;
        }
Rahul Wakade
  • 4,765
  • 2
  • 23
  • 25
0

I have created the collapsing UITableView

https://github.com/floriankrueger/iOS-Examples--UITableView-Combo-Box/zipball/master http://www.codeproject.com/Articles/240435/Reusable-collapsable-table-view-for-iOS

https://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html

it's working great..

Hiren
  • 12,720
  • 7
  • 52
  • 72
0

When you call row reload function does it enter the cellForRow delegate function? If it does then you should put some functionality to collapse the rows after checking for selected row.

Saleh
  • 380
  • 3
  • 19