天天看點

iOS開發UI篇—UITableview控件基本使用

一、一個簡單的英雄展示程式

njhero.h檔案代碼(字典轉模型)

iOS開發UI篇—UITableview控件基本使用
iOS開發UI篇—UITableview控件基本使用

njviewcontroller.m檔案代碼

iOS開發UI篇—UITableview控件基本使用
iOS開發UI篇—UITableview控件基本使用

實作效果:

iOS開發UI篇—UITableview控件基本使用

代碼注意點:

(1)在字典轉模型的代碼處用下面的代碼,為可變數組配置設定dictarray.count個存儲空間,可以提高程式的性能

nsmutablearray *models = [nsmutablearrayarraywithcapacity:dictarray.count];

(2)設定cell的高度

有三種辦法可以設定cell的高度

1) 可以在初始加載方法中設定,self.tableview.rowheight = 60;這适用于當每一行的cell高度一緻的時候,使用屬性設定cell的高度。

2)在storyboard中設定,适用于高度一緻

3)當每一行的cell的高度不一緻的時候就使用代理方法設定cell的高度

 - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

{

if (1 == indexpath.row) {

return 180;

}

return 44;

二、cell的一些屬性

代碼示例:

iOS開發UI篇—UITableview控件基本使用
iOS開發UI篇—UITableview控件基本使用
iOS開發UI篇—UITableview控件基本使用

cell的一些屬性:

(1)設定cell的輔助視圖,設定cell.accessoryview(系統提供了枚舉型,也可以自定義@父類指針指向子類對象);

(2)設定cell的背景顔色,有兩種方式可以設定cell的背景顔色:

通過backgroundcolor 和 backgroundview都可以設定cell的背景。但是backgroundview 的優先級比 backgroundcolor的高,是以如果同時設定了backgroundcolor和backgroundview, 那麼backgroundview會蓋住backgroundcolor

示例:cell.backgroundcolor = [uicolorbluecolor];

(3)設定cell預設狀态的背景

示例1:

   uiview *view = [[uiview alloc] init];

   view.backgroundcolor = [uicolor bluecolor];

  cell.backgroundview = view;

示例2:

uiimageview *iv = [[uiimageviewalloc] initwithimage:[uiimageimagenamed:@"buttondelete"]];

cell.backgroundview = iv;(父類指針指向子類對象,可以使用圖檔用簡單的操作設定絢麗的效果)

(4)設定cell選中狀态的背景

示例:

  uiview *view2 = [[uiview alloc] init];

view2.backgroundcolor = [uicolorpurplecolor];

cell.selectedbackgroundview = view2;

三、tableview的一些屬性

iOS開發UI篇—UITableview控件基本使用
iOS開發UI篇—UITableview控件基本使用
iOS開發UI篇—UITableview控件基本使用

tableview的一些屬性:

(1)設定分割樣式(tableview.separatorstyle),這是個枚舉類型

(2)設定分割線的顔色,可以直接使用系統給出的顔色,如果系統給定的顔色不能滿足需求時,也可以自定義。

  補充:顔色分為24位和32位的,如下

24bit顔色

r 8bit 0 ~ 255

g 8bit 0 ~ 255

b 8bit 0 ~ 255

32bit顔色

a 8bit 0 ~ 255(tou)

r 8bit

g 8bit

b 8bit

#ff ff ff 白色

#00 00 00 黑色

#ff 00 00 紅色

#255 00 00

設定為自定義顔色的執行個體:tableview.separatorcolor = [uicolorcolorwithred:0/255.0green:255/255.0blue:0/255.0alpha:255/255.0];

//接收的參數是顔色的比例值

(3)設定頂部和底部視圖

tableview.tableheaderview //頂部

tableview.tablefooterview //底部

繼續閱讀