如何让我的 RefreshControl 在 ViewDidAppear 期间出现 [英] How to make my RefreshControl appear during ViewDidAppear

查看:26
本文介绍了如何让我的 RefreshControl 在 ViewDidAppear 期间出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码相当简单 - 一个带有数据源的自定义表,用于从 Web 服务异步提供数据.

My code is fairly simple - A custom table with a datasource to provide data asynchronously from a web service.

为了获得最好的用户体验,我希望 UIRefreshControl 在此控制器出现时为加载过程设置动画,而不仅仅是在下拉列表时.

In order to get the nicest user experience I would like to have the UIRefreshControl animate the process of loading whenever this controller appears, instead of just when the list has been pulled down.

不幸的是,如果我在 ViewDidAppear 期间调用我的方法,UIRefreshControl 根本不会出现.

Unfortunately the UIRefreshControl does not appear at all if I call my method during ViewDidAppear.

我尝试了回答这些问题的建议,但似乎没有一个对我有用:

I've tried suggestions from answers these questions, but none of them seemed to work for me:

public partial class ControlCenterSelectionController : UITableViewController
{
    public ControlCenterSelectionController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        TableView.RefreshControl = new UIRefreshControl();
        TableView.RefreshControl.ValueChanged += RefreshControlOnValueChanged;
    }

    private async void RefreshControlOnValueChanged(object sender, EventArgs eventArgs)
    {
        await UpdateDataSourceAsync();
    }

    public override async void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        TableView.SetContentOffset(new CGPoint(0, -TableView.RefreshControl.Frame.Size.Height), true);
        await UpdateDataSourceAsync();
    }

    private async Task UpdateDataSourceAsync()
    {
        TableView.RefreshControl.BeginRefreshing();
        var ccClient = DI.Instance.Get<IControlCenterRestClient>();
        var controlCenters = await ccClient.GetAllAsync();
        var source = new GenericViewSource<ControlCenter>(controlCenters, item => item.Name, ControlCenterSelected);
        TableView.Source = source;
        TableView.RefreshControl.EndRefreshing();
    }

    private void ControlCenterSelected(ControlCenter controlCenter)
    {
        var controller = this.InstantiateController(Constants.ViewControllerIds.ControlCenter) as ControlCenterController;
        controller.ControlCenter = controlCenter;
        this.NavigateTo(controller);
    }
}

  • UITableView UIRefreshControl 不首先显示其视图时间
  • viewDidLoad 上的 UIRefreshControl
  • UIRefreshControl - 当 UITableViewController 在 UINavigationController 内时,beginRefreshing 不起作用
  • 我目前得到的只是一个看起来是空的屏幕,在没有指示的情况下加载数据,然后在完成后更新屏幕.

    All I currently get is a screen which looks empty, loads data without indication and then updates the screen once it's done.

    有人能发现错误吗?我真的找不到,因为这段代码很简单.

    Can someone spot an error? I can't really find one, since this code is rather simple.

    推荐答案

    绝望地将其发布到 SO + 绝望的编码尝试是关键:

    The desperation of posting it on SO + desperate coding attempts were key:

        public override async void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            TableView.SetContentOffset(new CGPoint(0, -(TableView.RefreshControl.Frame.Size.Height + 20)), true);
            TableView.RefreshControl.BeginRefreshing();
            await UpdateDataSourceAsync();
        }
    
        private async Task UpdateDataSourceAsync()
        {
            var ccClient = DI.Instance.Get<IControlCenterRestClient>();
            var controlCenters = await ccClient.GetAllAsync();
            var source = new GenericViewSource<ControlCenter>(controlCenters, item => item.Name, ControlCenterSelected);
            TableView.Source = source;
            TableView.RefreshControl.EndRefreshing();
        }
    

    这一变化尤其显着:

    -(TableView.RefreshControl.Frame.Size.Height + 20)
    

    事实证明,虽然在其他问题中,通过简单地应用基于 RefreshControl 的滚动会触发动画来解决该问题,但在我的情况下,我必须添加一些额外的 y 增量来触发动画.

    Turns out that, while in the other questions the issue was fixed by simply applying a scroll based on RefreshControl would fire the animation, in my case I had to add some extra y delta to make the animation fire.

    似乎只有在将扩展边设置为在顶部栏下方扩展时才需要这种额外的偏移.

    this extra offset seems to be required only if extended edges is set to extend under top bars.

    这篇关于如何让我的 RefreshControl 在 ViewDidAppear 期间出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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