官术网_书友最值得收藏!

Creating a "Pull to Refresh" mechanism in iOS

What if you want the user to be able to refresh the feed data in your table? You could create a regular button, or possibly check for new data after arbitrary time intervals. Alternatively, you can implement a cool "pull and refresh" mechanism, which has become the de facto standard refresh method in iOS.

In this recipe for our recipe finder app, we'll implement this very type of refresh mechanism for our recipes' feed using the built-in, native refresh control.

How to do it...

Open your recipes.js file and type the following under the creation of tblRecipes:

if (Ti.Platform.name === "iPhone OS") {
        var p2r = Ti.UI.createRefreshControl({
            tintColor: '#000'
        });

        tblRecipes.refreshControl = p2r;

        p2r.addEventListener('refreshstart', function(e) {

            refresh(function() {
                p2r.endRefreshing();
            });

        });
} else if (Ti.Platform.name === "android") {
        win.addEventListener("focus", refresh);
     }

Next, we need to modify the refresh function to support a callback that will run when the refresh is completed. Change the function definition to this:

function refresh(callback) {

Finally, add the following code before the closing of the function and after you set the table data:

if (typeof callback === 'function'){
              callback();
            }

Now launch the app and pull the recipe table down. You'll see the refresh spinner working, and the table will refresh!

How it works...

What we're doing here is using the built-in iOS refresh control and attach this to a table, and then telling the control what to do when it's pulled. We made our job easier by creating a refresh function, which can be passed to a callback function. So, when the refresh method is called, it refreshes the table and then calls the callback function, which in this case tells the refresh control to hide.

The last part of our code block checks for Android, intercepts the focus event of the window (which fires whenever the window is shown), and then calls the refresh function.

主站蜘蛛池模板: 探索| 乐平市| 通榆县| 莫力| 宁南县| 巴塘县| 重庆市| 夹江县| 宝清县| 湟源县| 神农架林区| 成安县| 达日县| 大田县| 阿合奇县| 卫辉市| 彩票| 建德市| 丰都县| 岳普湖县| 九江县| 梧州市| 葵青区| 靖西县| 江川县| 镇江市| 古交市| 辉县市| 惠东县| 弥渡县| 葵青区| 三原县| 九寨沟县| 石门县| 吉隆县| 左贡县| 华坪县| 晴隆县| 井研县| 汝南县| 奉化市|