MJRefresh刷新第三方庫

在查看這篇文章的時候,希望你能打開我新做的導(dǎo)航網(wǎng)站,將網(wǎng)站添加為自己瀏覽器的默認主頁,就當是對我的一種支持,URL:http://www.123xibei.com/

推薦一本ios的書吧,這個最新的oc api和swift語言結(jié)合講解,swift更容易上手,[精通ios開發(fā)第七版],【點擊購買】,強烈推薦。

先上圖:

1,下載地址:http://pan.baidu.com/s/1ntHWClj

2,導(dǎo)入到自己的工程中,如果是非ACR,則需要-fobjc-arc,如果是arc就不用管了。

一般的刷新都是基于UIScrollView的,因為能拖拽的view一般都是繼承于UIScrollView。

3,#import“MJRefresh.h” ? 這個可以直接加載.m中

然后.h文件中:

1

2

3

4

5

6

7

8

9#import

@interfaceMyViewController:UIViewController

{

CGRect_rect;//保存當前物理屏幕大小

}

@property(nonatomic,retain)UITableView *tableView;//當前的tableview

@property(nonatomic,retain)NSMutableArray *tableViewData;//tableview中數(shù)組數(shù)組

@end

.m文件中:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99#import "MyViewController.h"

#import "MJRefresh.h"

@interfaceMyViewController()

@end

@implementationMyViewController

-(id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil

{

self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if(self){

_rect=[[UIScreenmainScreen]bounds];

_tableViewData=[[NSMutableArrayalloc]init];

_tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,_rect.size.width,_rect.size.height)];

}

returnself;

}

-(void)viewDidLoad{

[superviewDidLoad];

for(inti=0;i<5;i++){

[_tableViewDataaddObject:[NSStringstringWithFormat:@"這是第%d條",i+1]];

}

_tableView.delegate=self;

_tableView.dataSource=self;

[self.viewaddSubview:_tableView];

//開啟刷新狀態(tài)

[selfsetupRefresh];

}

//

開始刷新自定義方法

-(void)setupRefresh

{

//下拉刷新

[self.tableViewaddHeaderWithTarget:selfaction:@selector(headerRereshing)dateKey:@"table"];

[self.tableViewheaderBeginRefreshing];

// 2.上拉加載更多(進入刷新狀態(tài)就會調(diào)用self的footerRereshing)

[self.tableViewaddFooterWithTarget:selfaction:@selector(footerRereshing)];

//一些設(shè)置

// 設(shè)置文字(也可以不設(shè)置,默認的文字在MJRefreshConst中修改)

self.tableView.headerPullToRefreshText=@"下拉可以刷新了";

self.tableView.headerReleaseToRefreshText=@"松開馬上刷新了";

self.tableView.headerRefreshingText=@"刷新中。。。";

self.tableView.footerPullToRefreshText=@"上拉可以加載更多數(shù)據(jù)了";

self.tableView.footerReleaseToRefreshText=@"松開馬上加載更多數(shù)據(jù)了";

self.tableView.footerRefreshingText=@"加載中。。。";

}

//下拉刷新

-(void)headerRereshing

{

//一般這些個里邊是網(wǎng)絡(luò)請求,然后會有延遲,不會像現(xiàn)在刷新這么快

// 1.添加假數(shù)據(jù)

[self.tableViewDatainsertObject:@"這是刷新的數(shù)據(jù)"atIndex:0];

[self.tableViewreloadData];

//2.結(jié)束刷新

[self.tableViewheaderEndRefreshing];

}

//上拉加載

-(void)footerRereshing

{

//這個一般是提取緩存的數(shù)據(jù)

// 1.添加假數(shù)據(jù)

[self.tableViewDatainsertObject:@"這是加載以前的數(shù)據(jù)"atIndex:[_tableViewDatacount]];

[self.tableViewreloadData];

//2,結(jié)束刷新

[self.tableViewfooterEndRefreshing];

}

//

#pragma mark - tableView delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return1;

}

-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section

{

return[_tableViewDatacount];

}

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath

{

staticNSString *cellID=@"cellID";

UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:cellID];

if(cell==nil){

cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID];

}

cell.textLabel.text=_tableViewData[indexPath.row];

returncell;

}

//釋放

-(void)dealloc

{

[_tableViewrelease];

[_tableViewDatarelease];

[superdealloc];

}

1

2如果在xcode6.0中有錯誤:

選中項目-Project-BuildSettings-AppleLLVM6.0-Preprocessing中的EnableStrictCheckingofobjc_msgsendcalls設(shè)置為NO即可

PS:

1

2

3

4

5

6

7

8//時間隊列,規(guī)定時間執(zhí)行某個事件

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2.0*NSEC_PER_SEC)),dispatch_get_main_queue(),^{

// 刷新表格

[self.tableViewreloadData];

// (最好在刷新表格后調(diào)用)調(diào)用endRefreshing可以結(jié)束刷新狀態(tài)

[self.tableViewheaderEndRefreshing];

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容