[IOS] using Uirefreshcontrol for UITableView pull-down refresh (Swift version)

Source: Internet
Author: User

First, initialize the relevant data in Viewdidload:


  Override Func Viewdidload () {        super.viewdidload ()        //Do any additional setup after loading the view.                Add Refresh        refreshcontrol.addtarget (self, Action: "RefreshData", forControlEvents:UIControlEvents.ValueChanged)        Refreshcontrol.attributedtitle = nsattributedstring (string: "Let go Refresh News")        Newstableview.addsubview ( Refreshcontrol)            }

So the drop-down refreshes are already visible:




Next, refine the selector and add the RefreshData method:

Refresh Data    func RefreshData () {let        bquery = Bmobquery (className: "News")        Bquery.findobjectsinbackgroundwithblock ({array, error in            Self.dataarray = array            Self.newsTableView.reloadData ()            self.refreshControl.endRefreshing ()}        )    }


The complete code is as follows, because it involves calls to the Bmob API, so some code may not be readable, but it does not affect the use of basic functionality.

newsviewcontroller.swift//womenworkerguide////Created on 9/20/14.//Copyright (c). All rights Reserved.//import uikit/*** news */class Newsviewcontroller:uiviewcontroller,uitableviewdelegate,        Uitableviewdatasource {@IBOutlet weak var newstableview:uitableview! var Refreshcontrol = Uirefreshcontrol () var dataarray:[anyobject] = [Anyobject] () override Func Viewdidload (        {super.viewdidload ()//Do any additional setup after loading the view. Self.automaticallyadjustsscrollviewinsets = false//Add refresh Refreshcontrol.addtarget (self, Action: "ref Reshdata ", forControlEvents:UIControlEvents.ValueChanged) Refreshcontrol.attributedtitle = nsattributedstring (stri NG: "Auto refresh after release") Newstableview.addsubview (Refreshcontrol) RefreshData ()} override Func Didreceivememory    Warning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.     }   Refresh Data func RefreshData () {Let bquery = Bmobquery (className: "News") BQUERY.FINDOBJECTSINBACKGROUNDW Ithblock ({array, error in Self.dataarray = array self.newsTableView.reloadData () self.ref Reshcontrol.endrefreshing ()})}//MARK:-uitableviewdatasource func TableView (Tableview:uitableview, n    Umberofrowsinsection section:int), Int {return dataarray.count;                } func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell { Let cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle, Reuseidentifier: "Newscell") le T obj:bmobobject = Dataarray[indexpath.row] as Bmobobject Cell.textlabel?. Text = Obj.objectforkey ("title") as?        String Let Dateformatter = NSDateFormatter () Dateformatter.dateformat = "yyyy year mm DD Day" Let str = dateformatter.stringfromdate (obj.createdat) CELl.detailtextlabel?.    Text = str return cell; }/*//MARK:-Navigation//In a storyboard-based application, you'll often want to do a Litt Le preparation before navigation override Func Prepareforsegue (segue:uistoryboardsegue!, sender:anyobject!)        {//Get the new view controller using Segue.destinationviewcontroller.    Pass the selected object to the new view controller. }    */}



[IOS] using Uirefreshcontrol for UITableView pull-down refresh (Swift version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.