iOS開發(fā)UI篇—UITableview控件簡單介紹_第1頁
iOS開發(fā)UI篇—UITableview控件簡單介紹_第2頁
iOS開發(fā)UI篇—UITableview控件簡單介紹_第3頁
iOS開發(fā)UI篇—UITableview控件簡單介紹_第4頁
iOS開發(fā)UI篇—UITableview控件簡單介紹_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論