-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNKScrollView+NSMutableArray.m
More file actions
executable file
·115 lines (92 loc) · 3.96 KB
/
NKScrollView+NSMutableArray.m
File metadata and controls
executable file
·115 lines (92 loc) · 3.96 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// NKScrollView+NSMutableArray.m
// Narongsak kongpan
//
// Created by Narongsak Kongpan on 1/25/13.
// Copyright (c) 2013 Narongsak kongpan. All rights reserved.
//
#import "NKScrollView+NSMutableArray.h"
@implementation UIScrollView (NKScrollView_NSMutableArray)
+(id)NKScrollView_BindArrayCellPage:(NSArray*)array initWithTarget:(id)target inScrollView:(UIScrollView *)scrollView pageControl:(UIPageControl *)pageView scrollTag:(NSString *)scrollTagName itemPerPage:(int)itemPerPage{
float scrollWidth = scrollView.frame.size.width;
float scrollHeight = scrollView.frame.size.height;
int itemCount = (int)[array count];
CGFloat maxColumnPerPage = 0;
//CGFloat maxRowPerPage = 0;
int indexInPage = 0;
int indexInRow = 0;
int currentPage = 0;
int currentRow = 0;
float cellWidth = 0;
float cellHeight = 0;
float contentHeight = 0;
for(UIView *item in [scrollView subviews]){
[item removeFromSuperview];
}
UIView *eachPageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollWidth, scrollHeight)];
[eachPageView setBackgroundColor:[UIColor clearColor]];
for (int count=0; count < itemCount; count++) {
NSDictionary *eachDict = [array objectAtIndex:count];
UIView *itemCell = [target performSelector:@selector(NKScrollViewCell:NSDictionary:)
withObject:scrollTagName
withObject:eachDict];
cellWidth = [itemCell frame].size.width;
cellHeight = [itemCell frame].size.height;
maxColumnPerPage = floor(scrollWidth / cellWidth);
//maxRowPerPage = ceil(scrollHeight / cellHeight);
int x = (cellWidth * indexInRow);
int y = (currentRow * cellHeight);
if(x > (scrollWidth - cellWidth)){
currentRow++;
indexInRow = 0;
x = 0;
y = (currentRow * cellHeight);
contentHeight += cellHeight;
}
[itemCell setFrame:CGRectMake(x, y, cellWidth, cellHeight)];
[eachPageView addSubview:itemCell];
if ([target respondsToSelector:@selector(NKScrollViewCell:attributes:)]) {
NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
[attrDict setValue:itemCell forKey:@"Cell"];
[attrDict setValue:eachDict forKey:@"Dict"];
[attrDict setValue:[NSString stringWithFormat:@"%i",currentPage] forKey:@"PageIndex"];
[attrDict setValue:[NSString stringWithFormat:@"%i",currentRow] forKey:@"RowIndex"];
[attrDict setValue:[NSString stringWithFormat:@"%i",count] forKey:@"SourceIndex"];
[attrDict setValue:[NSString stringWithFormat:@"%i",indexInPage] forKey:@"Index"];
[target performSelector:@selector(NKScrollViewCell:attributes:) withObject:scrollTagName withObject:attrDict];
}
indexInPage++;
indexInRow++;
if(indexInPage == itemPerPage){
indexInPage = 0;
currentRow = 0;
indexInRow = 0;
currentPage++;
[scrollView addSubview:eachPageView];
eachPageView = [[UIView alloc] initWithFrame:CGRectMake((currentPage * scrollWidth), 0, scrollWidth, scrollHeight)];
[eachPageView setBackgroundColor:[UIColor clearColor]];
}
}
if(indexInPage > 0){//Add Left View
[scrollView addSubview:eachPageView];
contentHeight += cellHeight;
currentPage++;
}
if (currentPage > 1) {
[scrollView setContentSize:CGSizeMake((scrollWidth * currentPage), scrollHeight)];
}else{
contentHeight = (itemCount / maxColumnPerPage);
contentHeight = ceil(contentHeight) * cellHeight;
if(contentHeight < scrollHeight){
contentHeight = scrollHeight;
}
[scrollView setContentSize:CGSizeMake(scrollWidth, contentHeight)];
eachPageView = [[scrollView subviews] objectAtIndex:0];
[eachPageView setFrame:CGRectMake(0, 0, scrollWidth, contentHeight)];
}
if(pageView != nil){
pageView.numberOfPages = currentPage;
}
return scrollView;
}
@end