在选择和取消选择时更改UITableViewCell的高度

时间:2013-02-28 04:31:49

标签: iphone uitableview

我有一个UITableView个单元格,在选中时显示弹出窗口,显示“是”或“否”确认,当选择“是”时,单元格的高度应增加到100及其背景图像应该改变,如果选择“否”,什么都不应该发生,它应该保持正常的细胞......

同样,如果选择了已选择的单元格,则再次显示“是”或“否”确认弹出窗口。如果选择“是”,则所选单元格应转换为高度为44的正常单元格,并删除背景图像。如果选择“否”,则没有任何反应,单元格仍然是选定单元格...

在这种情况下如何处理高度变化和背景图像变化?

#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。 - 您可以通过查看已注释的代码

来查看我为完成任务所做的尝试

3 个答案:

答案 0 :(得分:1)

为yes或no保留一个标志,并为所选行号保留一个索引。

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


 }

在创建单元格

时也类似地应用您的图像逻辑

答案 1 :(得分:1)

请发布您尝试过的代码。 这个链接可以帮到你。 Can you animate a height change on a UITableViewCell when selected?

使用弹出按钮索引更改变量值并在

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

        }
    }

答案 2 :(得分:0)

为两个casses创建两个tableViewCell。然后在buttonTouch方法中,使用

删除之前的单元格
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

然后使用以下方法添加newCell:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
相关问题