Clicked Header Section滚动到顶部 [英] Clicked Header Section is scroll to top

查看:101
本文介绍了Clicked Header Section滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下自定义headerview实现。当用户单击headerView时,它会展开以显示相应的单元格。

I have the following custom headerview implementation. When user clicks on headerView and it expands to show corresponding cells.

让我们假设我有两个部分。并且第一部分被扩展,第二部分被折叠。当我单击第二部分时,我希望此部分滚动到顶部。

Let's imagine I have two sections. And the first section is expanded, and second one is collapsed. When I click the second section, I want this section to scroll to the top.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ExpandableHeaderFooterView *sectionHeaderView = [self.comboTableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
    if (sectionHeaderView == nil)
    {
        sectionHeaderView = [[ExpandableHeaderFooterView alloc] initWithReuseIdentifier:SectionHeaderViewIdentifier];
    }
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectHeaderAction:)];
    [sectionHeaderView addGestureRecognizer:tapGesture];

    return sectionHeaderView;
}

-(void) selectHeaderAction :(UITapGestureRecognizer*) gestureRecognizer
{
    ExpandableHeaderFooterView* cell = (ExpandableHeaderFooterView*)gestureRecognizer.view;
    [self toggleSection:cell withSection: cell.section];
}

-(void)toggleSection:(ExpandableHeaderFooterView *)headerView withSection:(int)section
{
    ((ComboItem*)comboItemsArray[section]).expanded = !((ComboItem*)comboItemsArray[section]).expanded;

    [comboTableView beginUpdates];

    for (int i = 0; i< ((ComboItem*)comboItemsArray[section]).allComboItems.count; i++)
    {
        NSArray* rowsToReload = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:section], nil];
        [comboTableView reloadRowsAtIndexPaths:rowsToReload
                                   withRowAnimation:UITableViewRowAnimationFade];
    }

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];

    [comboTableView scrollToRowAtIndexPath:indexPath
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:YES];

    [comboTableView endUpdates];
}


推荐答案

因为Fade需要大约一半第二次执行,你会看到那个轻弹。您需要做的是给TableViewScroll稍微延迟,或者只使用:

Since Fade takes about half a second to execute, you will see that flick. What you need to do is either giving a small delay to TableViewScroll, or just using:

[CATransaction begin];
[comboTableView beginUpdates];
[CATransaction setCompletionBlock: ^{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];

   [comboTableView scrollToRowAtIndexPath:indexPath
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:YES];
}];
for (int i = 0; i< ((ComboItem*)comboItemsArray[section]).allComboItems.count; i++)
    {
        NSArray* rowsToReload = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:section], nil];
        [comboTableView reloadRowsAtIndexPaths:rowsToReload
                                   withRowAnimation:UITableViewRowAnimationFade];
    }
[comboTableView endUpdates];
[CATransaction commit];

这篇关于Clicked Header Section滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆