如何单独为tableviewcell图像创建动画?

时间:2017-07-25 13:28:38

标签: ios objective-c uitableview

我使用plist创建了扩展的tableview,我已将所有值和图像填充到我的tableview中。在这里,我想在用户点击该单元格时为我的特定单元格制作动画。

例如,第一次图像将是默认图像,下次如果单击该单元格,则特定单元格将被展开。在这里,我需要让我的形象隐藏;如果用户按下它将再次显示图像。怎么做?

3 个答案:

答案 0 :(得分:1)

每次用户点击一个单元格时,都会调用UITableViewDelegate方法,在这种方法中你可以重新加载单元格:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

重新加载单元格时,会调用以下方法(在其他单元格之间),您唯一需要做的就是根据单元格的当前状态返回不同的高度:

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (/* cell state */) ? 100.0 : 50.0;
}

您可以通过多种方式存储单元格的状态,例如,可以在其模型中执行此操作。

还有其他方法,这只是一个解决方案。但关键是你必须重新加载单元格,并根据你想要考虑的条件返回不同的高度。

答案 1 :(得分:0)

您可以在以下github

中查看我的完整解决方案

以下是我的一些实现。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    movies = [[NSArray alloc] initWithObjects:
              (NSArray*)[[Section alloc ] init:@"Istanbul" movieNames:@[@"Uskudar", @"Sariyer"] isExpanded:false],
              (NSArray*)[[Section alloc ] init:@"Bursa" movieNames:@[@"Osmangazi", @"Mudanya", @"Nilufer"]
               isExpanded:false],
              (NSArray*)[[Section alloc ] init:@"Antalya" movieNames:@[@"Alanya", @"Kas"] isExpanded:false], nil
              ];             
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return movies.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ((Section*)movies[section]).movies.count ;
}

-(void)toggleSection:(ExpandableHeaderFoorterView *)headerView withSection:(int)section
{
    ((Section*)movies[section]).expanded = !((Section*)movies[section]).expanded;
    [expandableTableView beginUpdates];
    for (int i= 0; i< ((Section*)movies[section]).movies.count; i++)
    {
        NSArray* rowsToReload = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:section], nil];
        [expandableTableView reloadRowsAtIndexPaths:rowsToReload
 withRowAnimation:UITableViewRowAnimationFade];
    }

    [expandableTableView endUpdates];

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ExpandableHeaderFoorterView* headerView = [[ExpandableHeaderFoorterView alloc] initWithCustom:((Section*)movies[section]).genre withSection:(int)section withDelegate:self];
    return (ExpandableHeaderFoorterView*)headerView;
}

答案 2 :(得分:-1)

http://www.iostute.com/2015/04/expandable-and-collapsable-tableview.html

您可以看到本教程。它是基于可扩展tableview单元格的非常好的教程。