-3

I have to get the IndexPath values to the NSMutableArray. I have to do an operation when a particular cell is selected at that time it's IndexPath is stored in NSMutableArray and particular cell height is increasing. When I am pressing again on the that cell at that time particular cell height is decreasing. How can it possible and what type of condition do I have to put?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
    selectIndexPathCell = indexPath;
    NSIndexPath *previousSelectedIndexPath = selectIndexPathCell;
    selectIndexPathCell = indexPath;

    if (previousSelectedIndexPath) {             
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousSelectedIndexPath]
                                       withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectIndexPathCell]
                 withRowAnimation:UITableViewRowAnimationAutomatic];

     self.selectedRow = sections_main[indexPath.section][@"content"][indexPath.row];
    [tblPeople beginUpdates];

    [tblPeople endUpdates];
}

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if(selectIndexPathCell != nil && [selectIndexPathCell compare:indexPath] == NSOrderedSame){
        return 80;
           }else{
               return 60.0;
}

}

Ankit
  • 286
  • 4
  • 16

2 Answers2

2

As I understood, you want criteria based table view cell height. You can manage it by keeping the corresponding cell's rowIndex in an array, and by using reloadData function heights can be managed under heightForRowAtIndexPath delegate, the cells height will be changed according to array values. Have a look at my suggestion here.

Hope it helps!

EDIT: (untested version to give you an idea)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(![arrayOfExpandedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]])
    {
        [arrayOfExpandedCellIndexes addObject:[NSNumber numberWithInteger:indexPath.row]];  // add if it does not exist
    }
    else
    {
        [arrayOfExpandedCellIndexes removeObject:[NSNumber numberWithInteger:indexPath.row]];  // remove if it exists
    }

    [tableView reloadData];  // reload data so height would adjust in `heightForRowAtIndexPath`
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([arrayOfExpandedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]])    
         return EXTENDED_CELL_HEIGHT;   // Macro : #define EXTENDED_CELL_HEIGHT 230.0f 
    else 
         return NORMAL_CELL_HEIGHT;     // Macro : #define NORMAL_CELL_HEIGHT   100.0f 
}

Assuming arrayOfExpandedCellIndexes is an NSMutableArray to maintain indicies.

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • I have to make cell which is expand and shrink when firsttime user will click on the cell at that time cell is expanding and second time user will click on that cell at time cell is shrink. only on particular cell which is selected by user. So, I have to manage it through NSMutableArray. How can i store selected index on NSMutableArray and reload the particular Cell. – Ankit Feb 26 '14 at 06:29
  • First thing `particular cell` can NOT be reloaded, using delegates(if custom cell) add/remove the index from the NSMutableArray and call `[yourTableView reloadData]` so when the data reloads it tooks correct height. So at start the cell index will not be in array, once user taps the cell `didSelectRowAtIndexPath` will call store value in array if not exists and remove if it exists. hope its clear now. – NeverHopeless Feb 26 '14 at 06:33
  • array1 = [[tableView cellForRowAtIndexPath:indexPath] mutableCopy]; now i get the data of particular indexpath ? – Ankit Feb 26 '14 at 06:35
  • Yes, when [didSelectRowAtIndexPath] clicked..Now what should i do. – Ankit Feb 26 '14 at 06:42
  • yes i already see that..but how can i get previously selected indexpath ? – Ankit Feb 26 '14 at 06:55
  • From your question it seems to me that making use of `arrayOfExpandedCellIndexes` will help. once the cell taped again for collapse, pick the index from array and remove it. What will you do if you get previous index ? – NeverHopeless Feb 26 '14 at 07:00
  • use `containsObject` instead. – NeverHopeless Feb 26 '14 at 07:12
  • Nope. still not solved. shrink and expand will not work. – Ankit Feb 26 '14 at 07:32
0
Take a global variable ex: Int selectedIndex; and now in didselectrow write this 
-(void)viewDidLoad  
{  
[super viewDidLoad];  
selectedIndex=50; // please assign it as your arraycount+1; which you are using to display on tableview  
}  
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

 if(selectedIndex==indexPath.row)
 {
   // if this is true you can do decrease your cell height
 selectedIndex=50; // please assign it as your arraycount+1;
 }
 else
 {
  // user can select one row and he can select another row without minimizing the previous one so you need to minimize the previous and increase new one. by using selectedIndex decrease cell height and by using indexPath.row increase height of cell
 selectedIndex=indexPath.row;

 }
[tableview reload];
 }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 if(selectedIndex==indexPath.row)
{
  return 60;
}
else
{
 return 30;
}

}
Charan Giri
  • 1,097
  • 1
  • 9
  • 15