iOS之创建表格类视图WBDataGridView

项目中创建表格, 引用头文件

#import "WBDataGridView.h"

  
 1 - (void)viewDidLoad{
 2 
 3     [superviewDidLoad];
 4 
 5     // Do any additional setup after loading the view.
 6 
 7     self.view.backgroundColor = [UIColorwhiteColor];
 8 
 9     
10 
11     CGFloat margin = 10.f;
12 
13     CGFloat width = self.view.frame.size.width -2*margin;
14 
15     
16 
17     // - 添加表格 - 两列
18 
19     WBDataGridView *DataGrid = [[WBDataGridViewalloc] initWithFrame:CGRectMake(margin,4*margin , width, 0)
20                                                         andColumnsWidths:@[@(width*0.4),@(width*0.6)]];
21 
22     DataGrid.roundCorner = YES;
23 
24     [DataGrid addRecord:@[@"姓名",@"dylan_lwb_"]];
25 
26     [DataGrid addRecord:@[@"性别",@""]];
27 
28     [DataGrid addRecord:@[@"电话",@"110119120"]];
29 
30     [DataGrid addRecord:@[@"邮箱",@"dylan_lwb@163.com"]];
31 
32     [self.viewaddSubview:DataGrid];
33 
34     // - 添加表格 - 多列
35 
36     WBDataGridView *MoreDataGrid = [[WBDataGridViewalloc]initWithFrame:CGRectMake(margin,CGRectGetMaxY(DataGrid.frame) +2*margin , width, 0)
37                                                             andColumnsWidths:@[@(width*0.2),@(width*0.2),@(width*0.2),@(width*0.4)]];
38 
39     MoreDataGrid.roundCorner = YES;
40 
41     [MoreDataGrid addRecord:@[@"姓名",@"姓名",@"姓名",@"dylan_lwb_"]];
42 
43     [MoreDataGrid addRecord:@[@"性别",@"性别",@"性别",@""]];
44 
45     [MoreDataGrid addRecord:@[@"电话",@"电话",@"电话",@"110119120"]];
46 
47     [MoreDataGrid addRecord:@[@"邮箱",@"邮箱",@"邮箱",@"dylan_lwb@163.com"]];
48 
49     [self.viewaddSubview:MoreDataGrid];
50 }

 

 1 //  WBDataGridView.h 
 2 
 3 #import <UIKit/UIKit.h>
 4 
 5 extern NSString *const SwitchButtonString;
 6  
 7 @interface WBDataGridView : UIView
 8 
 9 @property (retain,nonatomic) NSArray *columnsWidths;
10 
11 @property (assign,nonatomic) NSUInteger lastRowHeight;
12 
13 @property (retain,nonatomic) UIImage *selectedImage;
14 
15 @property (retain,nonatomic) UIImage *unselectedImage;
16 
17 @property (assign,nonatomic) BOOL roundCorner;
18 
19  
20 - (id)initWithFrame:(CGRect)frame andColumnsWidths:(NSArray*)columns;
21 
22 - (void)addRecord:(NSArray*)record;
23 
24 - (NSUInteger)selectedIndex;
25 
26  @end

 

  1 //  WBDataGridView.m 
  2 
  3 #import "WBDataGridView.h"
  4 
  5 NSString * const SwitchButtonString =@"SwitchButtonString";
  6 
  7 @interface WBDataGridView ()
  8 
  9 @property (assign,nonatomic) NSUInteger numRows;
 10 
 11 @property (assign,nonatomic) NSUInteger dy;
 12 
 13 @property (retain,nonatomic) NSMutableArray *switchButtons;
 14 
 15 @end
 16 
 17 @implementation WBDataGridView
 18 
 19 - (id)initWithFrame:(CGRect)frame andColumnsWidths:(NSArray*)columns{
 20 
 21     self = [superinitWithFrame:frame];
 22 
 23     if (self)
 24 
 25     {
 26 
 27         self.numRows =0;
 28 
 29         self.columnsWidths = columns;
 30 
 31         self.dy =0;
 32 
 33         self.numRows =0;
 34 
 35         self.switchButtons = [NSMutableArrayarray];
 36 
 37     }
 38     return self;
 39 }
 40 
 41 - (void)addRecord: (NSArray*)record
 42 
 43 {
 44     if(record.count !=self.columnsWidths.count)
 45 
 46     {
 47 
 48         NSLog(@"!!! Number of items does not match number of columns. !!!");
 49 
 50         return;
 51 
 52     }
 53 
 54     self.lastRowHeight =42;
 55 
 56     uint dx = 0;
 57 
 58 
 59     NSMutableArray* labels = [NSMutableArrayarray];
 60     
 61 
 62     // - create the items/columns of the row
 63 
 64     for(uint i=0; i<record.count; i++)
 65 
 66     {
 67 
 68         float colWidth = [[self.columnsWidthsobjectAtIndex:i] floatValue];//colwidth as given at setup
 69 
 70         CGRect rect = CGRectMake(dx, self.dy, colWidth,self.lastRowHeight);
 71 
 72         // - adjust X for border overlapping between columns
 73 
 74         if(i>0)
 75 
 76         {
 77 
 78             rect.origin.x -= i;
 79 
 80         }
 81 
 82         NSString *oneRecord = [record objectAtIndex:i];
 83 
 84         if ([oneRecord isEqualToString:SwitchButtonString])
 85 
 86         {
 87 
 88             // - set the switch button string as empty, create a label to adjust a cell first, then add the switch upon the label
 89 
 90             oneRecord = @"";
 91 
 92         }
 93 
 94         
 95 
 96         UILabel* col1 = [[UILabelalloc] init];
 97 
 98         [col1.layersetBorderColor:[[UIColorcolorWithWhite:0.821alpha:1.000]CGColor]];
 99 
100         [col1.layer setBorderWidth:1.0];
101 
102         col1.font = [UIFontfontWithName:@"Helvetica"size:self.numRows ==0 ? 14.0f :12.0f];
103 
104         col1.textColor = [UIColordarkGrayColor];
105 
106         col1.frame = rect;
107 
108 
109         // - round corner
110 
111         if ([selfisRoundCorner:i])
112 
113         {
114 
115             col1.layer.cornerRadius =5;
116 
117             col1.layer.masksToBounds =YES;
118 
119         }
120 
121         // - set left reght margins&alignment for the label
122 
123         NSMutableParagraphStyle *style =  [[NSParagraphStyledefaultParagraphStyle]mutableCopy];
124 
125         style.alignment =NSTextAlignmentCenter;
126 
127         NSAttributedString *attrText = [[NSAttributedStringalloc]initWithString:oneRecordattributes:@{NSParagraphStyleAttributeName : style}];
128 
129         col1.lineBreakMode =NSLineBreakByCharWrapping;
130 
131         col1.numberOfLines = 0;
132 
133         col1.attributedText = attrText;
134 
135         [col1 sizeToFit];
136 
137 
138         // - used to find height of longest label
139 
140         CGFloat h = col1.frame.size.height +10;
141 
142         if(h > self.lastRowHeight){
143 
144             self.lastRowHeight = h;
145 
146         }
147 
148         // - make the label width same as columns's width
149 
150         rect.size.width = colWidth;
151 
152         col1.frame = rect;
153 
154         [labels addObject:col1];
155 
156 
157         // - used for setting the next column X position
158 
159         dx += colWidth;
160 
161     }
162 
163     // - make all the labels of same height and then add to view
164 
165     for(uint i=0; i<labels.count; i++)
166 
167     {
168 
169         UILabel* tempLabel = (UILabel*)[labelsobjectAtIndex:i];
170 
171         CGRect tempRect = tempLabel.frame;
172 
173         tempRect.size.height =self.lastRowHeight;
174 
175         tempLabel.frame = tempRect;
176 
177         [self addSubview:tempLabel];
178 
179     }
180 
181     // - add the switch button at the first column in current row
182 
183     if ([record.firstObjectisEqualToString:SwitchButtonString])
184 
185     {
186 
187         UILabel *firstlabel = labels.firstObject;
188 
189         UIButton *oneSwitchButton = [[UIButtonalloc] initWithFrame:CGRectMake(0,0, [self.columnsWidths.firstObjectintegerValue], 40)];
190 
191         oneSwitchButton.center = firstlabel.center;
192 
193         [oneSwitchButton addTarget:selfaction:@selector(tapedSwitchButton:)forControlEvents:UIControlEventTouchUpInside];
194 
195         [oneSwitchButton setBackgroundImage:self.selectedImageforState:UIControlStateSelected];
196 
197         [oneSwitchButton setBackgroundImage:self.unselectedImageforState:UIControlStateNormal];
198 
199         [self.switchButtonsaddObject:oneSwitchButton];
200 
201         // - default selected first row button
202 
203         if (self.switchButtons.firstObject == oneSwitchButton)
204 
205         {
206 
207             oneSwitchButton.selected = YES;
208 
209         }
210         
211         [self addSubview:oneSwitchButton];
212 
213     }
214 
215     self.numRows++;
216     
217 
218     // - adjust Y for border overlapping beteen rows
219 
220     self.dy +=self.lastRowHeight-1;
221 
222     CGRect tempRect = self.frame;
223 
224     tempRect.size.height =self.dy;
225 
226     self.frame = tempRect;
227 }
228 
229 - (void)tapedSwitchButton:(UIButton *)button
230 
231 {
232     button.selected = !button.selected;
233 
234     [self.switchButtonsenumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {
235 
236         UIButton *oneButton = obj;
237 
238         if (oneButton != button)
239 
240         {
241 
242             oneButton.selected = NO;
243 
244         }
245     }];
246 }
247 
248 - (NSUInteger)selectedIndex
249 
250 {
251     __block NSUInteger index =0;
252 
253     [self.switchButtonsenumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {
254 
255         UIButton *oneButton = obj;
256 
257         if (oneButton.selected ==YES)
258 
259         {
260             index = idx;
261 
262             *stop = YES;
263         }
264     }];
265     return index;
266 }
267 
268 - (BOOL)isRoundCorner:(NSInteger)row
269 
270 {
271     return NO;
272 }
273 
274 @end
275 
276  
277  
278  

 

posted @ 2017-02-10 10:34  Jaycee麦子  阅读(556)  评论(0编辑  收藏  举报