2 回答

TA貢獻1875條經驗 獲得超5個贊
1、NSIndexPath
主要包含(NSUInteger)section
和(NSUInteger)row
,每個row
必然是屬于一個section
的,否則這個row
沒有意義,- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
里面就是表明各section
有多少row
。
2、打開Contacts
,里面的那些A、B、C...這樣的標題就屬于section header
,header + rows + footer
就構成了一個完整的section
. 可以通過- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
進行定制header,footer類似

TA貢獻1804條經驗 獲得超7個贊
先解釋一下TableView的布局,一個TableView的內容兩級,Section和Section中的Row,每個Row對應的視圖成為TableViewCell。拿 設置.app 舉例,如下圖:
圖中有兩個Section
Section 0 中有6個Row -- 飛行模式~運營商
Section 1 中能看到3個Row -- 聲音~桌面
再說問題里的兩個方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
這個方法返回的就是某個cell,可以自定義各種樣式渲染,也可以用幾種默認樣式去渲染。
indexPath標示的是你要渲染的這個cell的位置,indexPath對象里有兩個屬性,section和row,顧名思義,可以定位某個cell。
注意:這個方法會在某個cell出現在屏幕中的時候被調用,而且是沒出現一次就被調用一次。因為一個cell對象在屏幕可見區域消失的時候被回收,給其他出現在可見區域的cell復用。所以,在這個方法里渲染某個cell的時候,最好用個for循環或者什么的把cell清理干凈。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
這個方法,是在TableView生成的時候被調用(或者對tableView對象調用reloadData時),返回某個section中的row(cell)數量。
例如,在 設置.app 的界面里,應該是
switch (seciton) { 0 : return 6; 1 : return 3; }return 0;
添加回答
舉報