iOS UITableView

  1 .h文件
  2 #import <UIKit/UIKit.h>  
  3   
  4 @interface EXTVV2ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>  
  5   
  6 @end  
  7 //  
  8 //  EXTVV2ViewController.m  
  9 //  ExerciseTableViewV2  
 10 //  
 11 //  Created by hxl on 13-5-20.  
 12 //  Copyright (c) 2013年 xiaolei.hu. All rights reserved.  
 13 //  
 14   .m文件
 15 /* 
 16  UITableView 
 17  Tasks 
 18   
 19  //初始化UITableView对象 
 20  Initializing a UITableView Object 
 21   
 22  – initWithFrame:style: 
 23  - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 
 24   
 25   
 26  //配置UITableView 
 27  Configuring a Table View 
 28   
 29  //tableView的style 
 30  //UITableViewStylePlain或者UITableViewStyleGrouped,2者选1 
 31  style  property 
 32  @property(nonatomic, readonly) UITableViewStyle style 
 33   
 34  //当前section有多少行(此方法必须实现) 
 35  – numberOfRowsInSection: 
 36  - (NSInteger)numberOfRowsInSection:(NSInteger)section 
 37  //当前section的标示 
 38   
 39   
 40  //当前tableView里有多少section,默认为1 
 41  – numberOfSections 
 42  - (NSInteger)numberOfSections 
 43   
 44   
 45  //行高 
 46  rowHeight  property 
 47   
 48   
 49  separatorStyle  property 
 50  separatorColor  property 
 51  //tableview的背景 
 52  backgroundView  property 
 53   
 54   
 55   
 56  //创建cell 
 57  Creating Table View Cells 
 58  – registerNib:forCellReuseIdentifier: 
 59  – registerClass:forCellReuseIdentifier: 
 60  – dequeueReusableCellWithIdentifier:forIndexPath: 
 61  – dequeueReusableCellWithIdentifier: 
 62   
 63   
 64   
 65   
 66  Accessing Header and Footer Views 
 67  – registerNib:forHeaderFooterViewReuseIdentifier: 
 68  – registerClass:forHeaderFooterViewReuseIdentifier: 
 69  – dequeueReusableHeaderFooterViewWithIdentifier: 
 70  tableHeaderView  property 
 71  tableFooterView  property 
 72  sectionHeaderHeight  property 
 73  sectionFooterHeight  property 
 74  – headerViewForSection: 
 75  – footerViewForSection: 
 76  Accessing Cells and Sections 
 77  – cellForRowAtIndexPath: 
 78  – indexPathForCell: 
 79  – indexPathForRowAtPoint: 
 80  – indexPathsForRowsInRect: 
 81  – visibleCells 
 82  – indexPathsForVisibleRows 
 83  Scrolling the Table View 
 84  – scrollToRowAtIndexPath:atScrollPosition:animated: 
 85  – scrollToNearestSelectedRowAtScrollPosition:animated: 
 86  Managing Selections 
 87  – indexPathForSelectedRow 
 88  – indexPathsForSelectedRows 
 89  – selectRowAtIndexPath:animated:scrollPosition: 
 90  – deselectRowAtIndexPath:animated: 
 91  allowsSelection  property 
 92  allowsMultipleSelection  property 
 93  allowsSelectionDuringEditing  property 
 94  allowsMultipleSelectionDuringEditing  property 
 95  Inserting, Deleting, and Moving Rows and Sections 
 96  – beginUpdates 
 97  – endUpdates 
 98  – insertRowsAtIndexPaths:withRowAnimation: 
 99  – deleteRowsAtIndexPaths:withRowAnimation: 
100  – moveRowAtIndexPath:toIndexPath: 
101  – insertSections:withRowAnimation: 
102  – deleteSections:withRowAnimation: 
103  – moveSection:toSection: 
104  Managing the Editing of Table Cells 
105  editing  property 
106  – setEditing:animated: 
107  Reloading the Table View 
108  – reloadData 
109  – reloadRowsAtIndexPaths:withRowAnimation: 
110  – reloadSections:withRowAnimation: 
111  – reloadSectionIndexTitles 
112  Accessing Drawing Areas of the Table View 
113  – rectForSection: 
114  – rectForRowAtIndexPath: 
115  – rectForFooterInSection: 
116  – rectForHeaderInSection: 
117  Managing the Delegate and the Data Source 
118  dataSource  property 
119  delegate  property 
120  Configuring the Table Index 
121  sectionIndexMinimumDisplayRowCount  property 
122  sectionIndexColor  property 
123  sectionIndexTrackingBackgroundColor  property 
124   
125  */  
126   
127 #import "EXTVV2ViewController.h"  
128   
129 @interface EXTVV2ViewController ()  
130 @property (nonatomic) NSMutableArray *listData;  
131 @property (nonatomic) IBOutlet UITableView* myTableView;//在xib中与tableview控件关联  
132 @property (nonatomic) IBOutlet UISwitch* mySwitch;//在xib中与switch控件关联  
133 -(IBAction)switchEditModel:(UISwitch*)sender;//在xib中与switch控件的事件关联  
134 @end  
135   
136 @implementation EXTVV2ViewController  
137 @synthesize listData;  
138 @synthesize myTableView;  
139 @synthesize mySwitch;  
140   
141 - (void)viewDidLoad  
142 {  
143     [superviewDidLoad];  
144       
145 // Do any additional setup after loading the view, typically from a nib.  
146     [selfsetListData:[selfcreateData:26sectionRowLength:10stringLength:6]];  
147 }  
148   
149 - (void)didReceiveMemoryWarning  
150 {  
151     [superdidReceiveMemoryWarning];  
152     // Dispose of any resources that can be recreated.  
153 }  
154   
155 //指定有多少个分区(Section),默认为1  
156 /* 
157  1.此处根据二维数组外层的count获取section数量 
158  此时已有count个section被创建 
159  */  
160 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
161     return [self.listDatacount];  
162 }  
163   
164   
165 //指定各个分区中有多少行,默认为1。  
166 /* 
167  2.此处根据1设置的section数量获取数组二维内层长度(row数量)ps:section会根据你设置的最大值自动递增 
168  此时section对应的count个cell被创建 
169  */  
170 - (NSInteger) tableView: (UITableView *) tableView  
171   numberOfRowsInSection: (NSInteger) section {  
172     NSInteger rowCount = 0;  
173     //NSLog(@"section = %d",section);0/1/2  
174     if (section < self.listData.count) {  
175         rowCount = [self.listData[section]count];  
176     }  
177     return rowCount;  
178 }  
179   
180 //设置每行调用的cell  
181 /* 
182  3.此处根据1设置的section数量,和2设置的row数量获取数组内容并填充cell 
183  对1、2创建的容器进行填充,section和row就是二维数组的下标 
184  */  
185 - (UITableViewCell *) tableView: (UITableView *) tableView  
186           cellForRowAtIndexPath: (NSIndexPath *) indexPath  
187 {  
188     /* 
189      indexPath 索引路径 
190      property: 
191      row:table view 中 
192      item:collection view中 
193      section:table/collection view中 
194      method 
195      //collection view中 
196      + (NSIndexPath *)indexPathForItem:(NSInteger)item inSection:(NSInteger)section 
197      //table view 中 
198      + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section 
199       
200      */  
201       
202     //产生一个静态标示(每个cell形式相同可用)  
203     //static NSString * TableSampleIdentifier = @ "TableSampleIdentifier";  
204     //每个cell形式不相同需要不同标示  
205     NSString * TableSampleIdentifier = [[NSStringalloc]initWithFormat:@"CMainCell%d", indexPath.row];  
206       
207     //通过标示符获取一个cell对象(dequeueReusableCellWithIdentifier=>系统请求的回调函数)  
208     UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:  
209                               TableSampleIdentifier];  
210     //如果未获取到cell对象,创建新的cell对象,并赋予标示符  
211     if (cell == nil) {  
212         cell = [[UITableViewCellalloc]  
213                 initWithStyle:UITableViewCellStyleDefault  
214                 reuseIdentifier: TableSampleIdentifier];  
215     }  
216     NSString* cellText = nil;  
217     if (indexPath.section <self.listData.count) {  
218         NSArray* rowArray = self.listData[indexPath.section];  
219         if ([indexPath row] < rowArray.count) {  
220             cellText = rowArray[indexPath.row];  
221         }  
222     }  
223     cell.textLabel.text = cellText;  
224     return cell;  
225 }  
226   
227 //设置每个section显示的Title  
228 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
229 {  
230     NSString* title = nil;  
231     if (section < self.listData.count) {  
232         NSArray* rowArray = self.listData[section];  
233         if (rowArray.count >0) {  
234             //将每个section的第一行作为title是惯例  
235             title = rowArray[0];  
236         }  
237     }  
238     //截取首字母  
239     return [titlesubstringToIndex:1];  
240 }  
241   
242 //设置tableview每行的title(右侧索引)  
243 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
244     //initWithCapacity初始化数组时候指定长度  
245     NSMutableArray* indexTitleArray = [[NSMutableArrayalloc]initWithCapacity:[self.listDatacount]];  
246     //循环外围数组(section个数)  
247     for (UInt16 i =0; i < [self.listDatacount]; i++) {  
248         NSArray* rowArray = self.listData[i];  
249         //判断section下的数据行是否大于0  
250         if (rowArray.count >0) {  
251             NSString* titleStr = rowArray[0];  
252             //title长度超过3截取字符串  
253             if (titleStr.length >1) {  
254                 titleStr = [titleStr substringToIndex:1];  
255             }  
256             [indexTitleArray addObject:titleStr];  
257         }  
258     }  
259     //arrayWithArray产生一个新数组并释放原来的数组  
260     return [NSArrayarrayWithArray:indexTitleArray];  
261 }  
262   
263 //点击右侧索引时响应跳转到那个section的事件  
264 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index  
265 {  
266     return index;  
267 }  
268   
269   
270 //设置选中Cell的响应事件  
271 /* 
272  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
273  { 
274  [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失 
275  } 
276  */  
277   
278 //选中之前执行  
279 -(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath  
280 {  
281     return indexPath;  
282 }  
283   
284 //设置划动cell是否出现del按钮  
285 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath  
286 {  
287     return YES;  
288 }  
289   
290 //设置删除时编辑状态  
291 -(void)tableView:(UITableView *)tableView  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
292 {  
293     //删除元素的操作  
294     if (editingStyle ==UITableViewCellEditingStyleDelete)  
295     {  
296         //删除数据  
297         [self.listData[indexPath.section]removeObjectAtIndex:indexPath.row];  
298         //删除元素  
299         [tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationTop];  
300           
301     }  
302 }  
303   
304 //选中cell后触发的事件  
305 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
306 {  
307       
308     //设置选中的样式,4种风格 UITableViewCellAccessoryCheckmark UITableViewCellAccessoryDetailDisclosureButton  
309     //UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryNone  
310     UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];  
311     if (cellView.accessoryType ==UITableViewCellAccessoryNone) {  
312         cellView.accessoryType=UITableViewCellAccessoryCheckmark;  
313     }  
314     else {  
315         cellView.accessoryType =UITableViewCellAccessoryNone;  
316         [tableView deselectRowAtIndexPath:indexPathanimated:YES];  
317     }  
318       
319     //弹出框  
320     NSString *cellSelected=[self.listData[indexPath.section]objectAtIndex:indexPath.row];  
321     //indexPath.row得到选中的行号,提取出在数组中的内容。  
322     UIAlertView *myAlertView;  
323     myAlertView = [[UIAlertViewalloc]initWithTitle:@"你选中了:" message:cellSelected delegate:selfcancelButtonTitle:@"ok"otherButtonTitles:nil];  
324     //点击后弹出该对话框。  
325     [myAlertView show];  
326       
327 }  
328   
329 //是否能移动  
330 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {  
331     return YES;  
332 }  
333   
334 //移动操作  
335 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
336 {  
337     if (sourceIndexPath != destinationIndexPath) {  
338         id object = [self.listData[sourceIndexPath.section]objectAtIndex:sourceIndexPath.row];  
339         [self.listData[sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];  
340         if (destinationIndexPath.row > [self.listData[destinationIndexPath.section]count]) {  
341             [self.listData[destinationIndexPath.section]addObject:object];  
342         }  
343         else {  
344             [self.listData[destinationIndexPath.section]insertObject:objectatIndex:destinationIndexPath.row];  
345         }  
346     }  
347 }  
348   
349 //单元格返回的编辑风格,包括删除 添加和默认  和不可编辑三种风格  
350 //-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
351 //{  
352 //return UITableViewCellEditingStyleDelete;  
353 //return UITableViewCellEditingStyleNone;  
354 //return UITableViewCellEditingStyleInsert;  
355 //}  
356   
357 //switc按钮事件  
358 -(IBAction)switchEditModel:(UISwitch*)sender  
359 {  
360     //self.view.subview所有子视图,包括tableview等  
361     //是否开启编辑模式  
362     if(sender.on) {  
363         [self.myTableViewsetEditing:YESanimated:YES];  
364     } else {  
365         [self.myTableViewsetEditing:NOanimated:YES];  
366     }  
367       
368 }  
369   
370   
371 //生成随机字符串  
372 - (NSString *) createRandString:(NSInteger)stringLength perStr:(UInt16)pstr{  
373     UInt16 seed = 0;  
374     //97-122小写英语  
375     NSMutableString *str = [[NSMutableStringalloc]initWithFormat:@"%c", pstr];  
376     for(UInt16 i = 0; i < stringLength; i++) {  
377         seed = (arc4random() % 26) + 97;  
378         [str appendFormat:@"%c", seed];  
379     }  
380     return [NSStringstringWithString:str];  
381 }  
382   
383 - (NSMutableArray *) createData:(NSInteger)sectionLength sectionRowLength:(NSInteger)row stringLength:(NSInteger)length{  
384     NSMutableArray *sectionData = [[NSMutableArrayalloc]initWithCapacity:sectionLength];  
385     for (UInt16 i =0; i < sectionLength ; i++) {  
386         NSMutableArray* rowData = [[NSMutableArrayalloc]initWithCapacity:row];  
387         for (UInt16 j =0; j < row; j++) {  
388             [rowData addObject:[selfcreateRandString:lengthperStr:i +97]];  
389         }  
390         [sectionData addObject:rowData];  
391     }  
392     return sectionData;  
393 }  
394 @end

 

posted on 2014-04-12 15:38  XCoderLiu  阅读(377)  评论(0编辑  收藏  举报

导航