2013-02-28 69 views
0

我有一个UITableView单元格,其中选定单元显示弹出式窗口,显示“是”或“否”确认。选择“是”时,单元格高度应增加到100和它的背景图像应该改变,如果选择“否”,则不应该发生任何事情并且应该保持为正常单元格...在选择和取消选择时更改UITableViewCell的高度

类似地,如果已经选择了单元格,则它再次弹出“是“或”否“确认。如果选择“是”,则所选单元格应转换为高度为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. - 您可以通过查看已注释的代码来查看我为实现此任务所做的尝试。

+1

什么你试过吗?你面临什么问题?有没有错误或问题? – CRDave 2013-02-28 04:35:20

+0

嗨@CodeJack,请发布你已经尝试过的代码。这个链接可能会帮助你... http://stackoverflow.com/questions/460014/can-you-animate-a-height-change-on-a-uitableviewcell-when-selected。 – 2013-02-28 05:08:30

+0

我编辑了我的帖子,显示了我的代码.. – Subbu 2013-02-28 11:13:29

回答

0

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

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

移除旧的细胞,然后使用添加newCell:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 
+0

谢谢,我试过这种方法,它工作! 另外,有没有其他方法? – Subbu 2013-03-01 09:26:19

1

保留是或否的标志以及选定行号的索引。

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


} 

应用图像的逻辑,当您创建细胞

+0

先生,我试过这个东西,但这个东西隐藏在选定单元格下方的单元格... – Subbu 2013-02-28 11:15:04

+0

你可以给你的表格查看方法代码 – Durgaprasad 2013-02-28 12:19:30

+0

它的代码中显示。 – Subbu 2013-03-01 02:29:44

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) { 

     } 
    } 
相关问题