UITableView使用指南

本文轉(zhuǎn)自:http://steven.427.blog.163.com/blog/static/10236738020123264221032/

一、概述

UITableView是iOS開發(fā)比不可少也是最重要的一個(gè)控件類??梢哉f任何一個(gè)做iOS開發(fā)的人都必須熟練使用和掌握它。本文主要就是提供一個(gè)學(xué)習(xí)使用TableView的指南。

要說UITableView必須要介紹他的幾個(gè)親戚:UITableViewDelegate,UITableViewDataSource,UITableViewCell。其中前兩個(gè)是TableView遵守的兩個(gè)protocol(別告訴我你不知道啥叫protocol哦)。然后本文會(huì)再列出TableView最常用最重要的一些知識(shí)點(diǎn)。最后再介紹幾個(gè)參考例子。

二、UITableView和它的親戚們

1. UITableView

參考:

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html

1) 初始化 UITableView對(duì)象

– initWithFrame:style:// 代碼生成方式,如果你在nib里加的tableview不需要使用這個(gè)方法

2)配置TableView

– dequeueReusableCellWithIdentifier:// 必須要實(shí)現(xiàn)的方法,與TableView同生同死

styleproperty // 有兩種 UITableViewStylePlain, UITableViewStyleGrouped,經(jīng)常用

– numberOfRowsInSection://一個(gè)section有多少行,經(jīng)常用

– numberOfSections//一個(gè)TableView有多少個(gè)section,經(jīng)常用

rowHeightproperty // 行高,和tableView:heightForRowAtIndexPath:有性能上的區(qū)別

separatorStyleproperty // cell之間的分割線?待確認(rèn)

separatorColorproperty // 同上

backgroundViewproperty // tableview的背景view, 這個(gè)背景view在所有cell, header views, footer views之后

tableHeaderViewproperty // tableview上方的一個(gè)headerView, 和delete里的section header不是一個(gè)概念

tableFooterViewproperty // tableview下方的一個(gè)footerview

sectionHeaderHeightproperty // section Header的高度,

sectionFooterHeightproperty // sectjion Footer的高度

sectionIndexMinimumDisplayRowCountproperty // 功能待確認(rèn)? 參考例子:TheElements

3) 訪問Cells和Sections

– cellForRowAtIndexPath: //根據(jù)IndexPath返回cell

– indexPathForCell: //根據(jù)cell返回它的indexPath,和上面的方法互補(bǔ)

– indexPathForRowAtPoint://根據(jù)一個(gè)幾何點(diǎn)返回indexPath,如果超過邊界返回nil

– indexPathsForRowsInRect: //根據(jù)一個(gè)幾何的矩形返回矩形所覆蓋的行,返回是一個(gè)indexPath數(shù)組

– visibleCells // 不清楚怎么用,待確認(rèn)

– indexPathsForVisibleRows//同上

4) 滾動(dòng)TableView

– scrollToRowAtIndexPath:atScrollPosition:animated: // 滾動(dòng)到指定位置

– scrollToNearestSelectedRowAtScrollPosition:animated: // 同上

5) 管理sections

– indexPathForSelectedRow //返回選定行的indexPath,單行

– indexPathsForSelectedRows //返回選定行的indexPath數(shù)組,多行

– selectRowAtIndexPath:animated:scrollPosition: //根據(jù)indexPath選擇一行

– deselectRowAtIndexPath:animated: //反選一行,有何用?

allowsSelectionproperty //是否允許用戶選取一行

allowsMultipleSelectionproperty // 是否選取多行,缺省為NO. 可以試試YES后的效果,哈哈

allowsSelectionDuringEditingproperty // 編輯模式時(shí)是否可選取一行

allowsMultipleSelectionDuringEditingproperty // 編輯模式時(shí)可否選取多行

6) 插入、刪除、移動(dòng)行和sections

– beginUpdates // 和endUpdates一起用,讓插入、刪除、選擇操作同時(shí)動(dòng)畫,沒用過

– endUpdates //

– insertRowsAtIndexPaths:withRowAnimation: //根據(jù)indexPath數(shù)組插入行

– deleteRowsAtIndexPaths:withRowAnimation: //根據(jù)indexPath數(shù)組刪除行

– moveRowAtIndexPath:toIndexPath: //移動(dòng)一行到另一行

– insertSections:withRowAnimation: //插入sections

– deleteSections:withRowAnimation: //刪除sections

– moveSection:toSection: //移動(dòng)section

7) 管理和編輯cell

editingproperty // YES進(jìn)入編輯模式,tableview cell會(huì)出現(xiàn)插入、刪除、重排序的控件

– setEditing:animated: //設(shè)置進(jìn)入退出編輯模式

8) 重新加載TableView

– reloadData // 重建整個(gè)表,包括cells、header、footer,indexs

– reloadRowsAtIndexPaths:withRowAnimation: // 改進(jìn),不用reload整個(gè)表

– reloadSections:withRowAnimation: // 同上

– reloadSectionIndexTitles// 同上

9) 訪問TableView的畫圖區(qū)

– rectForSection: // 返回指定section的矩形

– rectForRowAtIndexPath: //返回indexPath指定行的矩形

– rectForFooterInSection: // 返回section的footer矩形

– rectForHeaderInSection: // 返回section的header矩形

10) Registering Nib Objects for Cell Reuse

– registerNib:forCellReuseIdentifier: //

11) 管理委托和數(shù)據(jù)源 (重要)

dataSourceproperty // 通常會(huì)這么用: myTableView.delegate = self; self 為viewController

delegateproperty // 通常會(huì)這么用: myTableView.dataSource = self; self 為viewController

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容