- Appcelerator Titanium Smartphone App Development Cookbook(Second Edition)
- Jason Kneen
- 320字
- 2021-07-30 10:09:37
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.

- ArchiCAD 19:The Definitive Guide
- 返璞歸真:UNIX技術(shù)內(nèi)幕
- 人工智能工程化:應(yīng)用落地與中臺(tái)構(gòu)建
- SharePoint 2010開發(fā)最佳實(shí)踐
- 高維聚類知識(shí)發(fā)現(xiàn)關(guān)鍵技術(shù)研究及應(yīng)用
- Nginx高性能Web服務(wù)器詳解
- Mastering Game Development with Unreal Engine 4(Second Edition)
- 激光選區(qū)熔化3D打印技術(shù)
- Bayesian Analysis with Python
- Linux Shell編程從初學(xué)到精通
- 空間機(jī)器人
- Building Google Cloud Platform Solutions
- Learning Couchbase
- 電氣自動(dòng)化工程師自學(xué)寶典(基礎(chǔ)篇)
- 網(wǎng)絡(luò)互聯(lián)組網(wǎng)配置技術(shù)