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);
UITableView
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門降狠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來对竣,“玉大人,你說我怎么就攤上這事榜配》裎常” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵蛋褥,是天一觀的道長临燃。 經(jīng)常有香客問我,道長烙心,這世上最難降的妖魔是什么膜廊? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮淫茵,結(jié)果婚禮上溃论,老公的妹妹穿的比我還像新娘。我一直安慰自己痘昌,他們只是感情好钥勋,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辆苔,像睡著了一般算灸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上驻啤,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼遥倦!你這毒婦竟也來了准脂?” 一聲冷哼從身側(cè)響起缓熟,我...
- 序言:老撾萬榮一對(duì)情侶失蹤弄捕,失蹤者是張志新(化名)和其女友劉穎侯谁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體堡称,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡瞎抛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了却紧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片桐臊。...
- 正文 年R本政府宣布懒浮,位于F島的核電站飘弧,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏砚著。R本人自食惡果不足惜次伶,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望稽穆。 院中可真熱鬧冠王,春花似錦、人聲如沸舌镶。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽餐胀。三九已至哟楷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間否灾,已是汗流浹背卖擅。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像扣汪,于是被迫代替她去往敵國和親断楷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 如果此文幫助了您,請點(diǎn)擊喜歡或評(píng)論,您的支持永遠(yuǎn)都是我前行的動(dòng)力. 本人在關(guān)于viewController中的添加...
- 點(diǎn)擊這里查看http://blog.sina.com.cn/s/blog_7b9d64af0101aaoy.html
- 我在開發(fā)的時(shí)候遇到了兩種情況私痹,第一種 刪除 一定要先把dataSource數(shù)組對(duì)應(yīng)的indexPath.row刪除...
- 出現(xiàn)這種狀況發(fā)現(xiàn) 是因?yàn)?沒有加 lanchimage的問題脐嫂,xcode 里沒有發(fā)現(xiàn) @3x的啟動(dòng)圖,會(huì)默認(rèn)為你沒...
- 我國有個(gè)成語叫“東施效顰”紊遵。 那是一個(gè)粉絲試圖通過模仿偶像的病癥來復(fù)制其成功(可惜失敗了)的故事。 該典故流傳經(jīng)年...