iOS UITableView
生活随笔
收集整理的這篇文章主要介紹了
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
?
轉載于:https://www.cnblogs.com/XCoderLiu/p/3660497.html
總結
以上是生活随笔為你收集整理的iOS UITableView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java设计模式--观察者模式(Obse
- 下一篇: 在ListBox中添加ToggleBut