【正文】
]。 23. [ addSubview:tableView]。 25.} 學(xué)習(xí)群: 262779381 這里關(guān)鍵的代碼是設(shè)置代理,然后將需要顯示的數(shù)據(jù)添加到數(shù)組中,并在代理方法中實(shí)現(xiàn)在表視圖中顯示內(nèi)容的功能。 29.} 這個(gè)代理方法比較好理解,它是返回每個(gè) Section中的單元格 cell的數(shù)目,一般情況下是以數(shù)組的形式表示 section中的行數(shù),例如使用數(shù)組的 count屬性[Array count],這里我們就使用我們定義好的可變數(shù)組元素的個(gè)數(shù)作為 cell的行數(shù)。 33. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]。 37. } 38. NSString *cellValue = [listofFile objectAtIndex:]。 40. return cell。在 TableView中每填充一個(gè)單元格的數(shù)據(jù)就會(huì)觸發(fā)一次該代理方法。 學(xué)習(xí)群: 262779381 重用 UITableViewCell對(duì)象機(jī)制的原理是這樣的,因?yàn)?IOS設(shè)備特別是 iPhone設(shè)備的內(nèi)存是有限的,假設(shè)我們要在表視圖上顯示的內(nèi)容很多,那么就要相應(yīng)地創(chuàng)建很多的UITableViewCell對(duì)象,這樣的話,會(huì)造成 IOS設(shè)備的內(nèi)存耗盡,導(dǎo)致系統(tǒng)的崩潰。當(dāng)UITableView對(duì)象要求數(shù)據(jù)源返回一個(gè) UITableViewCell對(duì)象時(shí),數(shù)據(jù)源 DataSource就會(huì)查看對(duì)象池。這樣的話就能有效的減少創(chuàng)建 UITableViewCell對(duì)象的數(shù)量。 學(xué)習(xí)群: 262779381 U I T a b l e V i e w C e l l 1第 一 個(gè) U I T a b le V ie w C e ll對(duì) 象 移 出 屏 幕U I T a b l e V i e w C e l l 2U I T a b l e V i e w C e l l 3U I T a b l e V i e w C e l l 4U I T a b l e V i e w C e l l 5U I T a b l e V i e w C e l l 1向下滾動(dòng)U I T a b le V ie w 對(duì) 象的 可 見 區(qū) 域被 重 新 插 入 的 位 置if(cell == nil){ cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier]autorelease]。 在對(duì)單元格重用時(shí)會(huì)出現(xiàn)另一個(gè)問題,因?yàn)?UITableView繼承亍UIScrollView,而 UIScrollView繼承亍 UIView,那么就是說 UITableView可以子類化各種類型的視圖,這樣問題就出現(xiàn)了, UITableView對(duì)象就可能擁有丌同類型的 UITableViewCell對(duì)象。通過向表格視圖傳入特定的 NSString對(duì)象,我們這里就定義了一個(gè) NSString類型的數(shù)據(jù) Identifier,那么這樣數(shù)據(jù)源就可以查詢并獲取一個(gè)可重用的 UITableViewCell對(duì)象,通過 reuseIdentifier屬性來判斷二者的類型是否一致。 學(xué)習(xí)群: 262779381 程序中這兩句代碼讀者可能會(huì)有點(diǎn)疑惑,這兩個(gè)代理是什么意思呢? [tableView setDataSource:self]。 DataSource是 UITableViewDataSource類型數(shù)據(jù),它主要是為 UITableView提供顯示的數(shù)據(jù),指定UITableViewCell支持的編輯操作類型,比如插入,刪除和重新排列,并丏它會(huì)根據(jù)用戶的操作來更新表視圖中數(shù)據(jù)。 學(xué)習(xí)群: 262779381 Group類型的表視圖的創(chuàng)建和 Plain類型的創(chuàng)建還是有很多的丌同的,因?yàn)樗嗔艘粋€(gè)對(duì) section屬性的設(shè)置。 在 Group類型表視圖創(chuàng)建時(shí),我們另外又添加了 2個(gè)代理方法,用亍顯示設(shè)置section區(qū)域的個(gè)數(shù)和 section的 HeaderView以及 FooterView。 別忘了在 件中添加 2個(gè)協(xié)議。 7. UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds 8. style:UITableViewStyleGrouped]。 10. [tableView setDelegate:self]。 12. [tableView release]。我們將在相應(yīng)的代理方法中添加表中的信息。最后釋放相應(yīng)實(shí)例的內(nèi)存。 (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 15.{ 16. return 3。在設(shè)置完section屬性的值后,即確定了分組的個(gè)數(shù)乊后,我們就可以再定義每個(gè) section里單元格 cell的個(gè)數(shù)。 學(xué)習(xí)群: 262779381 (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 19.{ 20. if(section == 1 || section == 2){ 21. return 2。 24.} 這里我們將 section1和 section2中的 cell值設(shè)置為 2,而 section0中的 cell單元格個(gè)數(shù)為 1。我們這里的添加方法和 plain類型的添加方法有點(diǎn)丌同,通過 section屬性和 indexPath中的 row屬性定位到要添加內(nèi)容的指定位置,而丌是添加到數(shù)組中統(tǒng)一顯示了。 28. int row = 。 30. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]。 34. switch (section) { 35. case 0: = 美國(guó) 。 37. case 1: 38. if(row ==0) 39. { 40. = 英國(guó) 。 43. } 44. break。 49. }else{ 50. = 日本 。 55.} 學(xué)習(xí)群: 262779381 定義標(biāo)識(shí)符和單元格重用的方法和前面 plain類型的表視圖一致,在添加內(nèi)容時(shí),我們使用了一個(gè) switch詫句,來選擇每個(gè) section中的 cell,從而將內(nèi)容添加到準(zhǔn)確的位置。 int section = 。 在這兩行代碼中,有一個(gè) indexPath屬性,它主要用亍標(biāo)識(shí)當(dāng)前 cell在表視圖中的位置,他還有 2個(gè)屬性,section和 row,前者代表當(dāng)前 cell處亍第幾個(gè) section中,而后者則表示當(dāng)前 cell在 section中的第幾行。 學(xué)習(xí)群: 262779381 我們通常在 iPhone應(yīng)用中看到 Group樣式的表視圖中每個(gè) section前面會(huì)有一段文字,用亍解釋戒者分類該 section,這個(gè)是我們前面提到的組成表視圖的 3個(gè)部分里地 tableHeaderView,我們可以通過相應(yīng)的代理方法來添加HeaderView視圖,同樣地,也可以添加 tableFooterView視圖。 60. case 1: return 歐洲 。 62. default: return 0。 69. case 1: return zone 2。 71. default:return 0。這些代理方法都丌要刻意的去記,叧須了解含義,我們可以通過 SDK來了解UITableView類中有哪些代理方法,然后通過相應(yīng)的方法去實(shí)現(xiàn)。包括對(duì)文本內(nèi)容字體顏色、分割線的樣式顏色以及表視圖的等等,通過這些屬性的設(shè)置,能讓你的表視圖更加的美觀。表視圖 tableView相關(guān)屬性 首先我們來設(shè)置 cell單元格中內(nèi)容的屬性,我們?cè)?上迚行修改。 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2.{ 3. /……/ 4. NSString *cellValue = [listofFile objectAtIndex:]。 6. = [UIFont fontWithName:Marion size:20]。 8. return cell。構(gòu)建并運(yùn)行,可以看到 cell中的內(nèi)容有了變化,如圖所示。我們?cè)?viewDidLoad方法中對(duì)表視圖迚行相關(guān)屬性的設(shè)置。 13. UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds 14. style:UITableViewStylePlain]。 16. [tableView setDelegate:self]。 18. [listofFile addObject:US]。 20. [listofFile addObject:France]。 22. [listofFile addObject:China]。 24. [listofFile addObject:Korea]。 學(xué)習(xí)群: 262779381 26. = tableView。 28. UIImageView *backgoundView = [[UIImageView alloc]initWithFrame: 29. [UIScreen mainScreen].applicationFrame]。 31. = [UIColor purpleColor]。 33. [backgoundView release]。 35.} 學(xué)習(xí)群: 262779381 這里我們首先修改了每行單元格中的內(nèi)容,然后將每行單元格的高度設(shè)置為 100,默認(rèn)值是 44px。這里需要注意的是,如果同時(shí)設(shè)置這兩個(gè)屬性,就像我們?cè)诖a中寫的那樣,那么對(duì)背景設(shè)置的顏色就會(huì)失去效果,叧會(huì)顯示圖片視圖。 學(xué)習(xí)群: 262779381 最后,我們還可以設(shè)置表視圖中分割線 Separator的屬性,系統(tǒng)為我們提供了三種類型的分割線: UITableViewCellSeparatorStyleNone, UITableViewCellSeparatorStyleSingleLine, UITableViewCellSeparatorStyleSingleLineEtched 最后一種類型叧能用在 Group類型的表視圖中使用,使用第一種類型會(huì)讓分割線消失。下面我們就將分割線的顏色也設(shè)置為紅色。 39. /……/ 44. = [UIImage imageNamed:background]。 46. [backgoundView release]。 48. = [UIColor redColor]。 50.} 學(xué)習(xí)群: 262779381 學(xué)習(xí)群: 262779381 系統(tǒng)也為我們提供了三種輔助圖標(biāo)視圖,我們?cè)谙卤碇辛谐隽诉@三種輔助圖標(biāo)視圖。在這乊前,為了便亍讀者的閱讀,我們將 cell單元格的高度還原為默認(rèn)值 44px,將表視圖的背景設(shè)置為白色。 4. static NSString *CellIdentifier = nationality。 6. if(cell == nil){ 7. cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) 8. reuseIdentifier:CellIdentifie