Use OC and Swift to create system-brought refresh interfaces

Source: Internet
Author: User

Use OC and Swift to create system-brought refresh interfaces

One: Swift Refresh interface code:

Import UIKit

Class Viewcontroller:uitableviewcontroller {

Data sources for display
var _datasource:[string] = []

Load more states Steam wheel
var _aiv:uiactivityindicatorview!

Override Func Viewdidload () {
Super.viewdidload ()
Additional setup after loading the view, typically from a nib.

Underlying data in the data source
For I in 0...2 {

_datasource.append ("\ (i)")
}

Initial drop-down refresh control
Self.refreshcontrol = Uirefreshcontrol ()
Self.refreshcontrol?. Attributedtitle = nsattributedstring (string: "Pull to Refresh")
Self.refreshcontrol?. Tintcolor = Uicolor.greencolor ()
Self.refreshcontrol?. AddTarget (Self, Action: "Refresh", forControlEvents:.) valuechanged)

Load more buttons in the background view
var Tablefooterview:uiview = UIView ()
Tablefooterview.frame = CGRectMake (0, 0, Self.view.bounds.size.width, 44)
Tablefooterview.backgroundcolor = Uicolor.greencolor ()
Self.tableView.tableFooterView = Tablefooterview

Load more Press-buttons
Let loadmorebtn = UIButton ()
Loadmorebtn.frame = CGRectMake (0, 0, Self.view.bounds.width, 44)
Loadmorebtn.settitle ("Load More", forstate:. Normal)
Loadmorebtn.settitlecolor (Uicolor.lightgraycolor (), forstate:. Normal)
Loadmorebtn.addtarget (Self, Action: "Loadmore:", forControlEvents:. Touchupinside)
Tablefooterview.addsubview (LOADMOREBTN)

Load more states Steam wheel
_aiv = Uiactivityindicatorview (activityindicatorstyle:. Gray)
_aiv.center = Loadmorebtn.center
Tablefooterview.addsubview (_AIV)
}

Load More methods
Func Loadmore (Sender:uibutton) {

Sender.hidden = True
_aiv.startanimating ()

Dispatch_async (dispatch_get_global_queue (0, 0), {()-Void in

Self._datasource.append ("\ (Self._datasource[self._datasource.count-1].toint ()! + 1) ")

Dispatch_async (Dispatch_get_main_queue (), {(), Void in

Sleep (1)

Self._aiv.stopanimating ()
Sender.hidden = False

Self.tableView.reloadData ()
})
})
}

Drop-Down Refresh method
Func Refresh () {

If Self.refreshcontrol?. refreshing = = true {

Self.refreshcontrol?. Attributedtitle = nsattributedstring (string: "Loading ...")
}

Dispatch_async (dispatch_get_global_queue (0, 0), {()-Void in

Self._datasource.insert ("\ (Self._datasource[0].toint ()! -1) ", atindex:0)

Dispatch_async (Dispatch_get_main_queue (), {(), Void in

Sleep (1)

Self.refreshcontrol?. Endrefreshing ()
Self.refreshcontrol?. Attributedtitle = nsattributedstring (string: "Pull to Refresh")

Self.tableView.reloadData ()
})
})
}

TableView DataSource
Override Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {

Return _datasource.count
}

Override Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {

Let identifier = "cell"

var cell = TableView. Dequeuereusablecellwithidentifier (identifier) as? UITableViewCell

if cell = = Nil {

Cell = UITableViewCell (style:. Default, Reuseidentifier:identifier)
}

Cell?. Textlabel?. Text = "\ (_datasource[indexpath.row])"

Return cell!
}
}

Second: OC Refresh interface Code:

#import "ViewController.h"

@interface Viewcontroller ()
{
Data source
Nsmutablearray * _DATASOURCE;

Wind Wheel
Uiactivityindicatorview * _AIV;
}

@end

@implementation Viewcontroller

-(Instancetype) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil
{
return self;
}

-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.

Initial data source
_datasource = [[Nsmutablearray alloc] init];

Basic data
for (int i=0; i<3; i++) {

[_datasource addobject:[nsstring stringwithformat:@ "%d", I]];
}

Refresh the control
Self.refreshcontrol = [[Uirefreshcontrol alloc] init];
Self.refreshControl.attributedTitle = [[Nsattributedstring alloc] initwithstring:@ "pull to Refresh"];
Self.refreshControl.tintColor = [Uicolor Greencolor];
[Self.refreshcontrol addtarget:self Action: @selector (refresh) forcontrolevents:uicontroleventvaluechanged];

Background view
UIView * Tablefooterview = [[UIView alloc] init];
Tablefooterview.frame = CGRectMake (0, 0, Self.view.bounds.size.width, 44);
Tablefooterview.backgroundcolor = [Uicolor Greencolor];
Self.tableView.tableFooterView = Tablefooterview;

Load more push buttons
UIButton * loadmorebtn = [[UIButton alloc] init];
Loadmorebtn.frame = CGRectMake (0, 0, Self.view.bounds.size.width, 44);
[Loadmorebtn settitle:@ "Load more" forstate:uicontrolstatenormal];
[Loadmorebtn Settitlecolor:[uicolor Lightgraycolor] forstate:uicontrolstatenormal];
[Loadmorebtn addtarget:self Action: @selector (Loadmore:) forcontrolevents:uicontroleventtouchupinside];
[Tablefooterview ADDSUBVIEW:LOADMOREBTN];

Wind Wheel
_AIV = [[Uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray];
_aiv.center = Loadmorebtn.center;
[Tablefooterview Addsubview:_aiv];
}

Load More methods
-(void) Loadmore: (UIButton *) sender
{
Sender.hidden = YES;
[_aiv startanimating];

Dispatch_async (dispatch_get_global_queue (0, 0), ^{

[_datasource addobject: [NSString stringwithformat:@ "%d", [_datasource[_datasource.count-1] intValue] + 1]];

Dispatch_async (Dispatch_get_main_queue (), ^{

Sleep (1);

[_aiv stopanimating];
Sender.hidden = NO;

[Self.tableview Reloaddata];
});
});
}

Drop-Down Refresh method
-(void) Refresh {

if (self.refreshControl.refreshing) {

Self.refreshControl.attributedTitle = [[Nsattributedstring alloc] initwithstring:@ "Loading ..."];
}

Dispatch_async (dispatch_get_global_queue (0, 0), ^{

[_datasource insertobject:[nsstring stringwithformat:@ "%d", [_datasource[0] intvalue]-1] atindex:0];

Dispatch_async (Dispatch_get_main_queue (), ^{

Sleep (1);

[Self.refreshcontrol endrefreshing];
Self.refreshControl.attributedTitle = [[Nsattributedstring alloc] initwithstring:@ "pull to Refresh"];

[Self.tableview Reloaddata];
});
});
}

TableView DataSource
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {

return _datasource.count;
}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

static NSString * identifier = @ "cell";

UITableViewCell * cell = [TableView dequeuereusablecellwithidentifier:identifier];

if (cell = = nil) {
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier];
}

Cell.textLabel.text = _datasource[indexpath.row];

return cell;
}

@end

Use OC and Swift to create system-brought refresh interfaces

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.