0

I have a UITableView with cells , which on selected shows a popup with "Yes" or "No" confirmation ,When "Yes" is selected , the height of cell should be increased to 100 and its background image should change and if "No" is selected , nothing should happen and it should remain as a normal cell...

Similarly , if already selected cell is selected , again it shows up popup of "Yes" or "No" confirmation . If "Yes" is selected , the selected cell should convert into a normal cell with height 44 and background image is removed . If "No" is selected , then nothing happens and cell remains as a selected cell...

how to deal with height changes and background image changes in such condition ????

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (id)initWithStyleUITableViewStyle)style
{
    self = [super initWithStyle:style];
   if (self)
   {
       objects = [NSArray   arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"       Ten",@"Eleven",@"Twelve",@"Thirteen", nil];
         selectedIndexPath = [[NSIndexPath alloc] init];
  }
   return self;
}




 - (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{

// Return the number of sections.
    return 1;
 }

 - (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
 {

    // Return the number of rows in the section.
    return [objects count];
 }

 - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath      *)indexPath
 {
     NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];
     UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseI  dentifier:CellIdentifier];
       cell.textLabel.text = [objects objectAtIndex:indexPath.row];

       NSLog(@"cell description:- %@",[cell description]);
   }


   return cell;


  }

   - (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
   {

  //    if (indexPath == selectedIndexPath)
  //    {
  //        return  100 ;
  //    }
  //    if (indexPath == selectedIndexPath)
  //    {
  //        UITableViewCell *c = [self.tableView cellForRowAtIndexPath:indexPath];
  //        CGRect rect = c.frame ;
  //        rect.size.height = 100 ;
  //        c.frame = rect ;
  //        return  100;
  //    }

      return  44;
 }

  -(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
 {
    if (selectedIndexPath == [self.tableView indexPathForSelectedRow])
     {
         UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"DeSelect" message:@"DeSelect ??" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"Yes", nil ];
         [a show];
    }
   else
  {
     UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Select" message:@"Select ??" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"Yes", nil ];
    [a show];
   }
}
 -(void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex
  {
       if (selectedIndexPath == [self.tableView indexPathForSelectedRow])
     {
         if (buttonIndex==1)
       {
         selectedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];
          // [self tableView:self.tableView heightForRowAtIndexPath:selectedIndexPath];
        //[self.tableView reloadData];
          //[self.tableView reloadRowsAtIndexPaths:[NSArray   arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        //[self.tableView beginUpdates];
        //[self.tableView endUpdates];
        [self showForIndexPath:selectedIndexPath];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];

    }
     }
     else
     {
       if (buttonIndex==1)
    {
        selectedIndexPath = [self.tableView indexPathForSelectedRow];
        //[self tableView:self.tableView heightForRowAtIndexPath:selectedIndexPath];
        //[self.tableView reloadData];
       //[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
      // [self.tableView beginUpdates];
     //  [self.tableView endUpdates];
          [self showForIndexPath:selectedIndexPath];
          [self.tableView beginUpdates];
          [self.tableView endUpdates];

      }
    }
    }
    -(void)showForIndexPathNSIndexPath *)indexpath
 {

    UITableViewCell *c = [self.tableView cellForRowAtIndexPath:indexpath];
    CGRect rect = c.frame ;
    rect.size.height = 100 ;
    c.frame = rect ;
    //code to move the cells downward when a cell is selected 
    for(int i = indexpath.row ; i < [objects count];i++)
   {
      NSindexpath *row = [NSIndexPath indexPAthforRow:i+1 in Section:0];
      UITableViewCell *tmp = [self.tableView cellforindexpath:row];
      CGRect frame = tmp.frame;
      CGpoint origin = frame.origin;
      CGFloat y = origin.y ;
      y = y+56; //(100-44)
      origin.y = y ;
      frame.origin = origin ;
      tmp.frame = frame ;
   }

    NSLog(@"Cell %d :- %@ indexpath:- %@",indexpath.row,[c description],indexpath);
 }

P.S. -- You can see the attempts I have made to achieve the task by looking at the code that is commented

Subbu
  • 2,063
  • 4
  • 29
  • 42
  • 1
    What you have tried? what problem you are facing? Is there any error or problem? – CRDave Feb 28 '13 at 04:35
  • Hi @CodeJack, please post the code what you have tried. This link may help you...http://stackoverflow.com/questions/460014/can-you-animate-a-height-change-on-a-uitableviewcell-when-selected. – Ramu Pasupuleti Feb 28 '13 at 05:08
  • i have edited my post showing up my code.. – Subbu Feb 28 '13 at 11:13

3 Answers3

1

Keep a flag for yes or no and a index for selected row number.

 -(CGFloat)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
      if ( flag == 1 && indexpath.row == index )
            return 50;  
      else
            return 30;


 }

apply your logic of image also similarly when you create cell

Durgaprasad
  • 1,910
  • 2
  • 25
  • 44
  • Sir , I tried out this thing , but this thing hides the cells below the selected cell ... – Subbu Feb 28 '13 at 11:15
1

please post the code what you have tried. This link may help you. Can you animate a height change on a UITableViewCell when selected?.

Use popup button index to change variable value and use that variable in

    -(CGFloat)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//change tablecell height based on variable value
}
    -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == 0) {

        }
    }
Community
  • 1
  • 1
Ramu Pasupuleti
  • 888
  • 2
  • 15
  • 35
0

Create two tableViewCell for the both casses. Then in your buttonTouch method, remove your previous cell using

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

And then add newCell using:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
rptwsthi
  • 10,094
  • 10
  • 68
  • 109