




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、iOS開發(fā)UI篇UITableview控件簡單介紹一、根本介紹在眾多移動(dòng)應(yīng)用中,能看到各式各樣的表格數(shù)據(jù) 。在iOS中,要實(shí)現(xiàn)表格數(shù)據(jù)展示,最常用的做法就是使用UITableView,UITableView繼承自UIScrollView,因此支持垂直滾動(dòng),且性能極佳 。UITableview有分組和不分組兩種樣式,可以在storyboard或者是用代碼設(shè)置。二、數(shù)據(jù)展示UITableView需要一個(gè)數(shù)據(jù)源(dataSource)來顯示數(shù)據(jù)UITableView會(huì)向數(shù)據(jù)源查詢一共有多少行數(shù)據(jù)以及每行顯示什么數(shù)據(jù)等 沒有設(shè)置數(shù)據(jù)源的UITableView只是個(gè)空殼但凡遵守UITableViewDa
2、taSource協(xié)議的OC對(duì)象,都可以是UITableView的數(shù)據(jù)源 展示數(shù)據(jù)的過程:1調(diào)用數(shù)據(jù)源的下面法得知一共有多少組數(shù)據(jù)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;2調(diào)用數(shù)據(jù)源的下面法得知每一組有多少行數(shù)據(jù)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 3調(diào)數(shù)據(jù)源的下法得知每顯示什么內(nèi)容- (UITableViewCell *)
3、tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;三、代碼例如1能根本展示的“垃圾代碼 1 #import "NJViewController.h" 2 3 interface NJViewController ()<UITableViewDataSource> 4 property (weak, nonatomic) IBOutlet UITableView *tableView; 5 6 end 7 8 implementation N
4、JViewController 9 10 - (void)viewDidLoad 11 12 super viewDidLoad; 13 / 設(shè)置tableView的數(shù)據(jù)源 14 self.tableView.dataSource = self; 15 16 17 18 #pragma mark - UITableViewDataSource 19 /* 20 * 1.告訴tableview一共有多少組 21 */ 22 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 23 24 NSLog("num
5、berOfSectionsInTableView"); 25 return 2; 26 27 /* 28 * 2.第section組有多少行 29 */ 30 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 31 32 NSLog("numberOfRowsInSection %d", section); 33 if (0 = section) 34 / 第0組有多少行 35 return 2; 36 else 37 38 / 第1組有
6、多少行 39 return 3; 40 41 42 /* 43 * 3.告知系統(tǒng)每一行顯示什么內(nèi)容 44 */ 45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 46 47 NSLog("cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row); 48 / indexPath.section; / 第幾組 49 / indexPath.row;
7、/ 第幾行 50 / 1.創(chuàng)立cell 51 UITableViewCell *cell = UITableViewCell alloc initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil; 52 53 / 2.設(shè)置數(shù)據(jù) 54 / cell.textLabel.text = "汽車" 55 / 判斷是第幾組的第幾行 56 if (0 = indexPath.section) / 第0組 57 if (0 = indexPath.row) / 第0組第0行 58 59 cell.textLabel.tex
8、t = "奧迪" 60 else if (1 = indexPath.row) / 第0組第1行 61 62 cell.textLabel.text = "寶馬" 63 64 65 else if (1 = indexPath.section) / 第1組 66 67 if (0 = indexPath.row) / 第0組第0行 68 cell.textLabel.text = "本田" 69 else if (1 = indexPath.row) / 第0組第1行 70 71 cell.textLabel.text = "
9、;豐田" 72 else if (2 = indexPath.row) / 第0組第2行 73 74 cell.textLabel.text = "馬自達(dá)" 75 76 77 78 / 3.返回要顯示的控件 79 return cell; 80 81 82 /* 83 * 第section組頭部顯示什么標(biāo)題 84 * 85 */ 86 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 87 88 / return "標(biāo)題&
10、quot; 89 if (0 = section) 90 return "德系品牌" 91 else 92 93 return "日韓品牌" 94 95 96 /* 97 * 第section組底部顯示什么標(biāo)題 98 * 99 */100 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section101 102 if (0 = section) 103 return "高端大氣上檔次"104 else105 10
11、6 return "還不錯(cuò)"107 108 109 end實(shí)現(xiàn)效果:2讓代碼的數(shù)據(jù)獨(dú)立新建一個(gè)模型 1 #import <Foundation/Foundation.h> 2 3 interface NJCarGroup : NSObject 4 /* 5 * 標(biāo)題 6 */ 7 property (nonatomic, copy) NSString *title; 8 /* 9 * 描述10 */11 property (nonatomic, copy) NSString *desc;12 /*13 * 當(dāng)前組所有行的數(shù)據(jù)14 */15 property (n
12、onatomic, strong) NSArray *cars;16 17 end 1 #import "NJViewController.h" 2 #import "NJCarGroup.h" 3 4 interface NJViewController ()<UITableViewDataSource> 5 property (weak, nonatomic) IBOutlet UITableView *tableView; 6 /* 7 * 保存所有組的數(shù)據(jù)(其中每一元素都是一個(gè)模型對(duì)象) 8 */ 9 property (nonato
13、mic, strong) NSArray *carGroups; 10 end 11 12 13 implementation NJViewController 14 15 16 #pragma mark - 懶加載 17 - (NSArray *)carGroups 18 19 if (_carGroups = nil) 20 / 1.創(chuàng)立模型 21 NJCarGroup *cg1 = NJCarGroup alloc init; 22 cg1.title = "德系品牌" 23 cg1.desc = "高端大氣上檔次" 24 cg1.cars = &
14、quot;奧迪", "寶馬" 25 26 NJCarGroup *cg2 = NJCarGroup alloc init; 27 cg2.title = "日韓品牌" 28 cg2.desc = "還不錯(cuò)" 29 cg2.cars = "本田", "豐田", "小田田" 30 31 NJCarGroup *cg3 = NJCarGroup alloc init; 32 cg3.title = "歐美品牌" 33 cg3.desc = "
15、價(jià)格昂貴" 34 cg3.cars = "勞斯萊斯", "布加迪", "小米" 35 / 2.將模型添加到數(shù)組中 36 _carGroups = cg1, cg2, cg3; 37 38 / 3.返回?cái)?shù)組 39 return _carGroups; 40 41 42 - (void)viewDidLoad 43 44 super viewDidLoad; 45 / 設(shè)置tableView的數(shù)據(jù)源 46 self.tableView.dataSource = self; 47 48 49 50 #pragma mark - U
16、ITableViewDataSource 51 /* 52 * 1.告訴tableview一共有多少組 53 */ 54 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 55 56 NSLog("numberOfSectionsInTableView"); 57 return self.carGroups.count; 58 59 /* 60 * 2.第section組有多少行 61 */ 62 - (NSInteger)tableView:(UITableView *)tableView
17、numberOfRowsInSection:(NSInteger)section 63 64 NSLog("numberOfRowsInSection %d", section); 65 / 1.取出對(duì)應(yīng)的組模型 66 NJCarGroup *g = self.carGroupssection; 67 / 2.返回對(duì)應(yīng)組的行數(shù) 68 return g.cars.count; 69 70 /* 71 * 3.告知系統(tǒng)每一行顯示什么內(nèi)容 72 */ 73 - (UITableViewCell *)tableView:(UITableView *)tableView cellFo
18、rRowAtIndexPath:(NSIndexPath *)indexPath 74 75 NSLog("cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row); 76 / indexPath.section; / 第幾組 77 / indexPath.row; / 第幾行 78 / 1.創(chuàng)立cell 79 UITableViewCell *cell = UITableViewCell alloc initWithStyle:UITableViewCellStyleDefault reuseIdent
19、ifier:nil; 80 81 / 2.設(shè)置數(shù)據(jù) 82 / cell.textLabel.text = "嗨嘍" 83 / 2.1取出對(duì)應(yīng)組的模型 84 NJCarGroup *g = self.carGroupsindexPath.section; 85 / 2.2取出對(duì)應(yīng)行的數(shù)據(jù) 86 NSString *name = g.carsindexPath.row; 87 / 2.3設(shè)置cell要顯示的數(shù)據(jù) 88 cell.textLabel.text = name; 89 / 3.返回要顯示的控件 90 return cell; 91 92 93 /* 94 * 第section組頭部顯示什么標(biāo)題 95 * 96 */ 97 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 98 99 / retu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 泉州工程職業(yè)技術(shù)學(xué)院《辦公空間室內(nèi)設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 馬鞍山職業(yè)技術(shù)學(xué)院《新型光纖通信系統(tǒng)》2023-2024學(xué)年第二學(xué)期期末試卷
- 信陽學(xué)院《中國傳統(tǒng)建筑設(shè)計(jì)研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 武漢體育學(xué)院《軟件設(shè)計(jì)模式》2023-2024學(xué)年第二學(xué)期期末試卷
- 齊齊哈爾工程學(xué)院《電力拖動(dòng)與運(yùn)動(dòng)控制》2023-2024學(xué)年第二學(xué)期期末試卷
- 泰安2025年山東泰安市屬事業(yè)單位初級(jí)綜合類崗位招聘127人筆試歷年參考題庫附帶答案詳解-1
- 內(nèi)蒙古建筑職業(yè)技術(shù)學(xué)院《普通化學(xué)原理》2023-2024學(xué)年第二學(xué)期期末試卷
- 山東協(xié)和學(xué)院《文藝節(jié)目策劃與創(chuàng)作》2023-2024學(xué)年第二學(xué)期期末試卷
- 蕪湖職業(yè)技術(shù)學(xué)院《現(xiàn)代教育技術(shù)理論及應(yīng)用》2023-2024學(xué)年第二學(xué)期期末試卷
- 河南2025年河南鄭州大學(xué)第一附屬醫(yī)院招聘博士302人筆試歷年參考題庫附帶答案詳解
- 電力系統(tǒng)動(dòng)態(tài)仿真與建模
- 庭院工程暫預(yù)算報(bào)價(jià)單(龍威景觀)
- 2024年南京機(jī)電職業(yè)技術(shù)學(xué)院單招職業(yè)技能測(cè)試題庫及答案解析
- 投標(biāo)技術(shù)服務(wù)和質(zhì)保期服務(wù)計(jì)劃
- 2023年全國高考體育單招考試英語試卷試題真題(精校打印版)
- 音樂欣賞與實(shí)踐(中職音樂)全套教學(xué)課件
- 粵語活動(dòng)策劃方案模板范文相關(guān)7篇
- 蘇教版三年級(jí)數(shù)學(xué)下冊(cè)教學(xué)計(jì)劃及進(jìn)度表
- 中國春節(jié)ppt英文版 Chinese New Year
- 高中數(shù)學(xué)《6.2 排列與組合》課件與導(dǎo)學(xué)案
- 腸道健康講座活動(dòng)策劃
評(píng)論
0/150
提交評(píng)論