editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)
生活随笔
收集整理的這篇文章主要介紹了
editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ios8 出來的左滑小菜單 可以自定義想要的按鈕 (要求ios8以上)
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {NSLog(@"-----------=點擊刪除");}];UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"編輯" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {NSLog(@"-----------=點擊編輯");}];UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置頂" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {NSLog(@"-----------=點擊置頂");}];UITableViewRowAction *signAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"標記未讀" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {NSLog(@"-----------=標記");}];// 毛玻璃效果deleteAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];editAction.backgroundColor = [UIColor blueColor];topAction.backgroundColor = [UIColor grayColor];signAction.backgroundColor = [UIColor yellowColor];return @[deleteAction, editAction, topAction, signAction]; }?
?
可以在導航欄右邊放編輯按鈕,刪除操作
-(void)delete:(UIBarButtonItem *)sender {if (self.tableView.editing == NO) {self.tableView.editing = YES;sender.title = @"完成";}else if (self.tableView.editing == YES) {self.tableView.editing = NO;sender.title = @"編輯";}}// 設置tableView是否可以編輯 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{return YES; } // 設置刪除操作時候的標題 -(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {return @"刪除"; }// 編輯模式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {return UITableViewCellEditingStyleDelete; }-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{if (editingStyle == UITableViewCellEditingStyleDelete) {[self.arrayM removeObjectAtIndex:indexPath.row];[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];}}?
導航欄右邊放編輯按鈕,插入操作
// 設置tableView是否可以編輯 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{return YES; }- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {return UITableViewCellEditingStyleInsert; }-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{if (editingStyle == UITableViewCellEditingStyleInsert) {// 我們實現的是在所選行的位置插入一行,因此直接使用了參數indexPathNSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];// 同樣,將數據加到list中,用的row[self.arrayM insertObject:@"新添加的行" atIndex:indexPath.row];[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];} }?
?
導航欄右邊放編輯按鈕,移動操作
// 設置編輯模式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {return UITableViewCellEditingStyleNone; }// 這個方法用來告訴表格 這一行是否可以移動 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {return YES; }// 這個方法就是執行移動操作的 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *) sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {NSUInteger fromRow = [sourceIndexPath row];NSUInteger toRow = [destinationIndexPath row];id object = [self.arrayM objectAtIndex:fromRow];[self.arrayM removeObjectAtIndex:fromRow];[self.arrayM insertObject:object atIndex:toRow]; }?
轉載于:https://www.cnblogs.com/Mr-Ygs/p/5630275.html
總結
以上是生活随笔為你收集整理的editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浮动、居中等笔记
- 下一篇: [HIHO1323]回文字符串(区间dp