Swift form TableView A feature implementation example of loading new data

Source: Internet
Author: User
Tags current time

For tables (TableView), Drop-down refresh data, pull-load data should be the two most commonly used data update operations. For the former, I originally wrote a related article: Swift-Drop-down to refresh the functionality of the data implementation (using Uirefreshcontrol). This time I talk about the implementation of the latter.
Say is pull load data, in fact, when we scroll the table content to the last row, the system will automatically get new content and add to the tail of the list (the specific effect can refer to the app Baidu Bar). Below we demonstrate the implementation of the pull load by using a sample example.

1, Sample Effect chart

(1) The first time to enter the program, first load the first 20 data.
(2) when the TableView scroll bar is moved to the bottom, you will see that the bottom of the table has a loading prompt and continue loading the next 20 data.
(3) When the data is loaded, the new data is added to the bottom of the table.
(4) If the table is rolled to the bottom again, loop the above operation and continue loading the new data.

2, the principle of realization

(1) The key point of pull load is to decide when to start loading new data. Here we start loading automatically when the last item of data is displayed. That is, when the user pulls the form, it is loaded when the last piece of data is seen.
(2) At the same time we add a hint view to the tablefooterview of the table to tell the user that the data is loading.
(3) Each time the data is received, the new data is added to the local data collection, and the TableView Reloaddata () method is called again to refresh the table data.
(4) Be aware that every time a request is initiated, a protection is made to ensure that the original request does not return without initiating a new request. Prevents users from scrolling back and forth on the last page causing multiple requests.
(5) The actual project this data is requested through the network. This is directly generated locally and uses a timer to add a delay to better see the effect.

3, complete code

Import Uikit

Class Viewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {

Table View
@IBOutlet weak var tableview:uitableview!

Data collection
var dataarray = [String] ()

The view at the bottom of the table to prompt for data loading
var loadmoreview:uiview?

Counter (used to simulate the network load effect of delay)
var timer:nstimer!

The record is currently allowed to load new data (it is set to False when it is loaded, and a duplicate load is placed)
var loadmoreenable = True

Override Func Viewdidload () {
Super.viewdidload ()

Self.tableView.delegate = Self
Self.tableView.dataSource = Self

Pull up Refresh
Self.setupinfinitescrollingview ()
Self.tableView.tableFooterView = Self.loadmoreview

Load data for the first time
Loadmore ()
}

Refresh View on Pull
Private Func Setupinfinitescrollingview () {
Self.loadmoreview = UIView (Frame:cgrectmake (0, Self.tableView.contentSize.height,
Self.tableView.bounds.size.width, 60))
self.loadmoreview!. Autoresizingmask = Uiviewautoresizing.flexiblewidth
self.loadmoreview!. BackgroundColor = Uicolor.orangecolor ()

Add an intermediate loop progress bar
Let Activityviewindicator = Uiactivityindicatorview (activityindicatorstyle:. White)
Activityviewindicator.color = Uicolor.darkgraycolor ()
Let Indicatorx = self.loadmoreview!. Frame.size.width/2-activityviewindicator.frame.width/2
Let indicatory = self.loadmoreview!. Frame.size.height/2-activityviewindicator.frame.height/2
Activityviewindicator.frame = CGRectMake (Indicatorx, Indicatory,
ActivityViewIndicator.frame.width,
ActivityViewIndicator.frame.height)
Activityviewindicator.startanimating ()
self.loadmoreview!. Addsubview (Activityviewindicator)
}

Return number of records
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
return dataarray.count;
}

Cell Data Settings
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell {
Set cell data
Let cell = Tableview.dequeuereusablecellwithidentifier ("cell", Forindexpath:indexpath)
Cell.textlabel? Text = Self.dataarray[indexpath.row]

Now pull to the bottom, execute loadmore ()
if (loadmoreenable && indexpath.row = = self.dataarray.count-1) {
Loadmore ()
}

return cell
}

Load more data
Func Loadmore () {
Print ("Load new data!") ")
Loadmoreenable = False
Timer = nstimer.scheduledtimerwithtimeinterval (2.0, Target:self,
Selector: #selector (viewcontroller.timeout), Userinfo:nil, Repeats:true)
}

Timer time to
Func TimeOut () {
Let start = Self.dataArray.count + 1
Randomly add 10 new data (time is current time)
For I in start.. <start+20 {
Self.dataArray.append ("News headlines \ (i)")
}
Self.tableView.reloadData ()
Loadmoreenable = True

Timer.invalidate ()
Timer = Nil
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Related Article

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.