UITableView使用技巧總結(刪除,多選,滑動等)

一.進行重載數據的API

注意點:在操作這些方法的時候,記得要修改和保存數據來源,根據測試,該方法調用之后,會調用返回組數和行數的數據源方法(不會調用返回cell的數據源方法).倘若不同步刷新和更改數據會造成真實反正行/組合實際添加/刪除后的行/組不匹配而奔潰.

插入某行/某組數據:

-(void)insertSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以插入整個章節(jié)

-(void)insertRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;

刪除某行某族數據:

-(void)deleteSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以刪除整個章節(jié)

-(void)deleteRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;

刷新(重置)某行數據

-(void)reloadSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAniamtion)animation;//在iPhoneos 3.0中重載章節(jié)數據(上面兩個方法的合并)它們能重載部分的數據,而不會把所有東西都丟掉

-(void)reloadRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;

二.設置和使用系統(tǒng)的cell樣式

-(id)initWithStyle:(UITableviewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

三.系統(tǒng)自帶的UITableView的插入和刪除

第三方庫的代表:https://github.com/MortimerGoro/MGSwipeTableCell

實現UITableView的對應數據源和代理方法.

開啟UITableView的編輯模式

使用技巧:[self.tableViewsetEditing:!_tableView.editinganimated:YES];,這樣可以設置按鈕點擊的編輯和關閉.

- (void)setEditing:(BOOL)editing animated:(BOOL)animate;/** TableView 進入或退出編輯狀態(tài)(TableView 方法). */

確定哪些行的cell可以編輯 (UITableViewDataSource協議中方法),設置為Yes的時候當當前行編輯模式為刪除的時候可滑動刪除.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

設置cell側滑的文字

-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath

返回側滑中的項目,這個方法只有返回按鈕為刪除樣式(無返回樣式的時候才有用),當這個方法調用了,返回刪除按鈕文字的方法和,點擊原本刪除editingStyleForRowAtIndexPath方法不再調用.

- (nullableNSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath

舉個栗子:(可以根據indexpath來定制不同的返回樣式)

- (nullableNSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath{

UITableViewRowAction*collectRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"收藏么么噠"handler:^(UITableViewRowAction*_Nonnullaction,NSIndexPath*_NonnullindexPath) {

NSLog(@"點擊后的回調,不會走commitEditingStyle方法");

}];

collectRowAction.backgroundColor= [UIColororangeColor];

return@[collectRowAction];

}

設置某一行cell的編輯模式 (UITableViewDelegate)協議中方法,不同行可以設置不同的編輯模式,主要為刪除和添加(如果不實現,默認為刪除)

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

選中刪除(或插入)狀態(tài)之后的操作(數據源進行更新, cell刪除或插入)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

舉個栗子:

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

/**點擊刪除按鈕的操作*/

if(editingStyle ==UITableViewCellEditingStyleDelete){

/**<判斷編輯狀態(tài)是刪除時. */

/** 1.更新數據源(數組):根據indexPaht.row作為數組下標,從數組中刪除數據. */

[self.arr removeObjectAtIndex:indexPath.row];

/** 2. TableView中刪除一個cell. */

[tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationRight]; }

/**點擊+號圖標的操作. */

if(editingStyle ==UITableViewCellEditingStyleInsert){

/**<判斷編輯狀態(tài)是插入時. *//** 1.更新數據源:向數組中添加數據. */

[self.arr insertObject:@"abcd"atIndex:indexPath.row];

/** 2. TableView中插入一個cell. */

[tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

? }

}

UITableView cell的多選和全選功能:

真正項目中做刪除

1.將表中的cell刪除

2.將本地的數組中數據刪除

3.最后將服務器端的數據刪除

多選的步驟

1.允許多行選中self.tableView.allowsMultipleSelectionDuringEditing = YES;開啟編輯模式(同上第一點).

2.選擇編輯處理方式,默認為刪除,這樣選擇,會返回空心的小圓.

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{

? ? ? ? ? ? ?returnUITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;

}

更新數據來源默認為刪除,這樣選擇,會返回空心的小圓.

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

? ? ? ? ?//self.deleteArr用來定義存放多選選中的cell,indexpath,這里無需手動調用取消選中cell的方法

? ? ? ? ? [self.deleteArraddObject:indexPath];

}

- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath? {

//將數據移除 ? ? ? ? ?

? ? ? ? ? ?[self.deleteArrremoveObject:indexPath];

}

然后在點擊確認刪除后使用刪除cell的方法(-(void)deleteRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;)進行刪除self.deleteArr中選中的數據.

(注意記得先刪除數據來源中的數據,這里可以可以使用直接刪除數據來源數組中對應的數據,然后調用reloadData方法,但是這里沒有動畫),這樣的話,self.deleteArr中應該保存的是數據來源數組中的數據[self.deleteArraddObject:[self.dataArrayobjectAtIndex:indexPath.row]];,然后調用([self.deleteArr addObjectsFromArray:self.dataArray];)這個方法

全選功能:

在全選按鈕點擊后(打開編輯狀態(tài)),遍歷所有數據,調用selectRowAtIndexPath方法,選中多有的cell,同時將全部數據添加到要刪除的數組中

for(inti =0; i<self.dataArray.count;i++){

? ? ? ? ?NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

? ? ? ? ?[self.tableView selectRowAtIndexPath:indexPath animated:YESscrollPosition:UITableViewScrollPositionTop];

? ? ? ? ?[self.deleteArr addObjectsFromArray:self.dataArray];

}

參考:http://www.reibang.com/p/ec6a037e4c6b

四.UITableView cell 的移動

開啟UITableView的編輯模式

使用技巧:[self.tableViewsetEditing:!_tableView.editinganimated:YES];,這樣可以設置按鈕點擊的編輯和關閉.

- (void)setEditing:(BOOL)editing animated:(BOOL)animate;/** TableView 進入或退出編輯狀態(tài)(TableView 方法). */

指定UITableView哪些行可以移動

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

移動cell

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

舉個栗子:

- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath

{

/**? 1.從原位置移除,在從原位置移除之前,需要保存一下原位置的數據,同時持有一次. */

NSString*str = [self.dataArrobjectAtIndex:sourceIndexPath.row];

[self.dataArrremoveObjectAtIndex:sourceIndexPath.row];

/** 2.添加到目的位置,同時釋放一次*/

[self.dataArrinsertObject:stratIndex:destinationIndexPath.row];

}

防止不同分區(qū)間的移動

- (NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath

- (NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath{if(sourceIndexPath.section == proposedDestinationIndexPath.section) {returnproposedDestinationIndexPath;? ? }else{returnsourceIndexPath;? ? }}

五.UITableView cell 的將要繪制和消失時調用的兩個方法

將要出現的時候調用,他的調用順序在cellforRow方法之后,所以,這個方法,倘若你沒有來得及在cellForRow中賦值,還可以在這個方法中救場.這個方法可以用來優(yōu)化性能,在這里可以監(jiān)聽cell是不是最后一組的最后一行,來判斷是否滑動到了tableView的底部(這個在cell的自動計算中拿不到真實contentSize的時候作用巨大)

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath;

cell消失的時候,調用

- (void)tableView:(UITableView*)tableView didEndDisplayingCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath;

六.UITableView cell 傳按鈕事件給UITableViewController的方法

1.一般來講,我們可以使用代理或者是Block,但是在有些時候,我們的cell特殊的在其他地方很少用到,甚至不會用到,我們可以直接在

控制器中使用addTarget的方法來簡化代碼.(在這里需要拿到按鈕所在的cell的indexpath)

先展示兩個重要的方法

根據indexpath返回tableView對應的cell

- (nullable__kindofUITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath;

根據cell返回其對應的indexPath

- (nullableNSIndexPath*)indexPathForCell:(UICollectionViewCell*)cell;

這里提供兩種解決思路:

1.綁定按鈕的Tag

在cellforRow方法中對cell中按鈕綁定tag,當然這里的用的,使用于cell中只有一個按鈕的時候.

可以對btn如此設置:

NSString* cellStr = [NSString stringWithFormat:@"%d", indexPath.row];

[imgBtn setTitle:cellStr forState:UIControlEventTouchCancel];

然后在btn的action函數里邊這樣調用:

NSString* cellIndex = [imgBtn titleForState:UIControlEventTouchCancel];

取[cellIndex intValue]即可

剝離cell的結構,這個一定要,對自己的cell結構了解,才能取到其cell

NSIntegerremoveRowN = [self.tableViewindexPathForCell:((YGMemberCell*)[[sendersuperview]superview])].row;//這個方便一點點,


待續(xù)~~~~~~~~~~~~~~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末隘竭,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子歧焦,更是在濱河造成了極大的恐慌签赃,老刑警劉巖哨坪,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贩幻,死亡現場離奇詭異轿腺,居然都是意外死亡,警方通過查閱死者的電腦和手機丛楚,發(fā)現死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門吃溅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鸯檬,你說我怎么就攤上這事÷莨福” “怎么了喧务?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長枉圃。 經常有香客問我功茴,道長,這世上最難降的妖魔是什么孽亲? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任坎穿,我火速辦了婚禮,結果婚禮上返劲,老公的妹妹穿的比我還像新娘玲昧。我一直安慰自己,他們只是感情好篮绿,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布孵延。 她就那樣靜靜地躺著,像睡著了一般亲配。 火紅的嫁衣襯著肌膚如雪尘应。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天吼虎,我揣著相機與錄音犬钢,去河邊找鬼。 笑死思灰,一個胖子當著我的面吹牛玷犹,可吹牛的內容都是我干的。 我是一名探鬼主播官辈,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼遍坟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了晴股?” 一聲冷哼從身側響起愿伴,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎电湘,沒想到半個月后隔节,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡寂呛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年怎诫,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贷痪。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡幻妓,死狀恐怖,靈堂內的尸體忽然破棺而出劫拢,到底是詐尸還是另有隱情肉津,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布舱沧,位于F島的核電站妹沙,受9級特大地震影響,放射性物質發(fā)生泄漏熟吏。R本人自食惡果不足惜距糖,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牵寺。 院中可真熱鬧悍引,春花似錦、人聲如沸缸剪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杏节。三九已至唬渗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間奋渔,已是汗流浹背镊逝。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嫉鲸,地道東北人撑蒜。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親座菠。 傳聞我的和親對象是個殘疾皇子狸眼,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內容