一.進行重載數據的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ù)~~~~~~~~~~~~~~