UITableView



UITableView 總結(jié)



UITableView是UIScrollView的子類坝撑,因此它可以自動(dòng)響應(yīng)滾動(dòng)事件(一般為上下滾動(dòng))渴庆。
它內(nèi)部包含0到多個(gè)UITableViewCell對(duì)象灯蝴,每個(gè)table cell展示各自的內(nèi)容杨刨。
當(dāng)新cell需要被顯示時(shí),就會(huì)調(diào)用tableView:cellForRowAtIndexPath:方法來獲取或創(chuàng)建一個(gè)cell;
而不可視時(shí),它又會(huì)被釋放父款。
由此可見,同一時(shí)間其實(shí)只需要存在一屏幕的cell對(duì)象即可瞻凤,不需要為每一行創(chuàng)建一個(gè)cell憨攒。 

兩種樣式:group 和 plain

1.協(xié)議介紹

UITableViewDataSource(11)

//每個(gè)section下cell的個(gè)數(shù)(必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//通過indexpath返回具體的cell(必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
 
//返回有多少個(gè)section(默認(rèn)是1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
   //每個(gè)section上面的標(biāo)語內(nèi)容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//每個(gè)section下面的標(biāo)語內(nèi)容
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
 
// Editing
//是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
 
// Moving/reordering
// 是否可拖拽
-tableView:moveRowAtIndexPath:toIndexPath:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
 

// Data manipulation - insert and delete support
// 對(duì)Cell編輯后的回調(diào)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
 
// 對(duì)Cell拖拽后的回調(diào)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
UITableViewDelegate(常用)

// Variable height support
// 每個(gè)cell高度的返回(這里高度通過協(xié)議返回,是為了table能準(zhǔn)確的定位出要顯示的Cell-index阀参,從而滿足UITableView的重用機(jī)制)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 每個(gè)section-header高度的返回
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
// 每個(gè)section-footer高度的返回
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
 
// Section header & footer information. Views are preferred over title should you decide to provide both
//可返回每個(gè)section-header的自定義視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height
//可返回每個(gè)section-footer的自定義視圖
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height
 
// Selection
 


// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
UITableViewDelegate中的協(xié)議還有很多肝集,我只列出了比較常用的,想知道更多的可以查看官方頭文件或官方文檔

 



2.下拉刷新 上拉加載
MJRefresh

3.重用
UItableView對(duì)Cell有一套重用機(jī)制蛛壳,他會(huì)將滾出屏幕外的cell放到一個(gè)隊(duì)列中杏瞻,滾入屏幕的會(huì)從這個(gè)隊(duì)列中獲取cell,如果沒有再去創(chuàng)建衙荐。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellIdentifier = @"cellIdentifier ";

   

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:windowReuseIdentifier];

        if (!cell) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:windowReuseIdentifier];

        }

    return cell;

}   

自定義cell

新建cell文件捞挥,繼承UITableViewCell

static NSString *cellid = @"cellIdentifier";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
     
    if (!cell) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
3. 如果有xib

static NSString *CellIdentifier = @"FriendCell";
        FriendCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            [tableView registerNib:[UINib nibWithNibName:@"FriendCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }


不使用重用方法

方法1 將獲得cell的方法從- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

 

重用機(jī)制調(diào)用的就是dequeueReusableCellWithIdentifier這個(gè)方法,方法的意思就是“出列可重用的cell”忧吟,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell)砌函,就可以不使用重用機(jī)制,因而問題就可以得到解決瀑罗,雖然可能會(huì)浪費(fèi)一些空間胸嘴。

 第一個(gè)方法如果使用下面插入多次可能會(huì)有問題:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
方法2 通過為每個(gè)cell指定不同的重用標(biāo)識(shí)符(reuseIdentifier)來解決雏掠。

重用機(jī)制是根據(jù)相同的標(biāo)識(shí)符來重用cell的斩祭,標(biāo)識(shí)符不同的cell不能彼此重用。于是我們將每個(gè)cell的標(biāo)識(shí)符都設(shè)置為不同(@"CMainCell%d", indexPath.row)

 

5.編輯

//是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

 //返回編輯的類型1.沒有2.刪除3.插入
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;



.盡量不要老調(diào)用reloaddata乡话,可能的情況下可以考慮使用

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);
 
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
-(void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);


 

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末摧玫,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子绑青,更是在濱河造成了極大的恐慌诬像,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件闸婴,死亡現(xiàn)場離奇詭異坏挠,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)邪乍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門降狠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來对竣,“玉大人,你說我怎么就攤上這事榜配》裎常” “怎么了?”我有些...
    開封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵蛋褥,是天一觀的道長临燃。 經(jīng)常有香客問我,道長烙心,這世上最難降的妖魔是什么膜廊? 我笑而不...
    開封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮淫茵,結(jié)果婚禮上溃论,老公的妹妹穿的比我還像新娘。我一直安慰自己痘昌,他們只是感情好钥勋,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辆苔,像睡著了一般算灸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上驻啤,一...
    開封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天菲驴,我揣著相機(jī)與錄音,去河邊找鬼骑冗。 笑死赊瞬,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的贼涩。 我是一名探鬼主播巧涧,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼遥倦!你這毒婦竟也來了准脂?” 一聲冷哼從身側(cè)響起缓熟,我...
    開封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤弄捕,失蹤者是張志新(化名)和其女友劉穎侯谁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體堡称,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瞎抛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了却紧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片桐臊。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡钞艇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出豪硅,到底是詐尸還是另有隱情哩照,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布懒浮,位于F島的核電站飘弧,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏砚著。R本人自食惡果不足惜次伶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望稽穆。 院中可真熱鬧冠王,春花似錦、人聲如沸舌镶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽餐胀。三九已至哟楷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間否灾,已是汗流浹背卖擅。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留墨技,地道東北人惩阶。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像扣汪,于是被迫代替她去往敵國和親断楷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容