iOS開發UITableView基本使用方法總結1

2022-05-03 15:09:23 字數 4838 閱讀 4682

uitableview基本使用方法

1.首先,controller需要實現兩個delegate ,分別是uitableviewdelegate 和uitableviewdatasource

2.然後 uitableview物件的 delegate要設定為 self。

3.然後就可以實現這些delegate的一些方法拉。

(1)- (nsinteger)numberofsectionsintableview:(uitableview *)tableview;

這個方法返回 tableview 有多少個section

//返回有多少個sections  

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview  

(2)- (nsinteger)tableview:(uitableview *)table numberofrowsinsection:(nsinteger)section;

這個方法返回   對應的section有多少個元素,也就是多少行。

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section  

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

這個方法返回指定的 row 的高度。

- (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section;

這個方法返回指定的 section的header view 的高度。

- (cgfloat)tableview:(uitableview *)tableview heightforfooterinsection:(nsinteger)section;

這個方法返回指定的 section的footer view 的高度。

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath  

// configure the cell.  

cell.textlabel.text=@"簽名";  

cell.detailtextlabel.text = [nsstring stringwithcstring:userinfo.user_signature.c_str() encoding:nsutf8stringencoding];  

}(5)- (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section

返回指定的 section 的header的高度

- (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section  

(6)- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section

返回指定的section 的 header  的 title,如果這個section header  有返回view,那麼title就不起作用了。

- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section  

else if (section == 1)  

else  

}  else  

}  (7) - (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section

返回指定的 section header 的view,如果沒有,這個函式可以不返回view

- (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section  

}  (8)  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath

當使用者選中某個行的cell的時候,**用這個。但是首先,必須設定tableview的乙個屬性為可以select 才行。

tableview.allowsselection=yes; 

cell.selectionstyle=uitableviewcellselectionstyleblue;  

如果不希望響應select,那麼就可以用下面的**設定屬性:

tableview.allowsselection=no;  

下面是響應select 點選函式,根據哪個section,哪個row 自己做出響應就好啦。

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath  

else if(indexpath.section==0)  

break;  

default:  

break;  

}  }  

else  

}  如何讓cell 能夠響應 select,但是選中後的顏色又不發生改變呢,那麼就設定

cell.selectionstyle = uitableviewcellselectionstylenone;

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath  

{  //cell被選中後的顏色不變  

cell.selectionstyle = uitableviewcellselectionstylenone;  

(9)如何設定tableview  每行之間的分割線

self.tableview.separatorstyle=uitableviewcellseparatorstylesingleline;  

如果不需要分割線,那麼就設定屬性為 uitableviewcellseparatorstylenone  即可。

(10)如何設定 tableview cell的背景顏色

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath  

{  //設定背景顏色  

cell.contentview.backgroundcolor=[uicolor colorwithred:0.957 green:0.957 blue:0.957 alpha:1];  

}  這個函式響應,使用者點選cell 右邊的 箭頭(如果有的話)

(12)如何設定tableview 可以被編輯

[tableview setediting:yes animated:yes];  

如果要退出編輯模式,肯定就是設定為no

- (uitableviewcelleditingstyle)tableview:(uitableview *)tableview editingstyleforrowatindexpath:(nsindexpath *)indexpath

返回當前cell  要執行的是哪種編輯,下面的**是返回刪除模式

- (uitableviewcelleditingstyle)tableview:(uitableview *)tableview editingstyleforrowatindexpath:(nsindexpath *)indexpath  

-(void) tableview:(uitableview *)atableview

commiteditingstyle:(uitableviewcelleditingstyle) editingstyle

forrowatindexpath:(nsindexpath *)indexpath

通知告訴使用者編輯了 哪個cell,對應上面的**,我們在這個函式裡面執行刪除cell的操作。

-(void) tableview:(uitableview *)atableview  

commiteditingstyle:(uitableviewcelleditingstyle) editingstyle  

forrowatindexpath:(nsindexpath *)indexpath  

{  [chatarray removeobjectatindex:indexpath.row];  

[chattableview reloaddata];  

(13)如何獲得 某一行的cell物件

- (uitableviewcell *)cellforrowatindexpath:(nsindexpath *)indexpath;

iOS開發小技巧 重新整理UITableView

現在就來看看,重新整理uitableview該怎麼做吧,一般情況下,我們會通過直接呼叫reloaddata的方法,去重新整理uitableview的。重新整理uitableview self.tableview reloaddata reloaddata是重新整理整個uitableview,有時候,...

iOS開發小技巧 重新整理UITableView

今天我們來看看如何重新整理uitableview的,一般情況下,重新整理uitableview,我們會直接呼叫reloaddata方法。self.tableview reloaddata reloaddata是重新整理整個uitableview,有時候,我們可能需要區域性重新整理。比如 只重新整理乙...

mac開發 ios開發

但是,任何乙個作業系統上,只使用開發語言就去開發程式是不行的。還需要有介面庫。尤其是支援object c的介面庫。mac上使用oc開發應用程式,都會使用xcode這個ide,整合開發工具,xcode中整合了gui介面庫。可以直接拖動控制項到介面上。objective c是一門語言,而cocoa 是這...