#import "AppDelegate.h"
#include "RootTableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];
return YES;
}
#import <UIKit/UIKit.h>
@interface RootTableViewController : UITableViewController
//存儲數據的可變集合
@property(strong,nonatomic) NSMutableArray *Contacts;
// 記錄選中行的索引值
@property(strong,nonatomic) NSIndexPath *CurrentIndexPath;
#import "RootTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"側滑按鈕";
self.tableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"106.jpg"]];
//右編輯
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//數據源
self.Contacts=[NSMutableArray array];
[self.Contacts addObject:@"劉木"];
[self.Contacts addObject:@"燕江"];
[self.Contacts addObject:@"唐明芳"];
[self.Contacts addObject:@"王榮政"];
[self.Contacts addObject:@"張雲江"];
[self.Contacts addObject:@"劉茂旭"];
//唯一標識
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MyCell"];
}
#pragma mark - Table view data source
//多少個分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Incomplete implementation, return the number of sections
return 1;
}
//每個分區多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete implementation, return the number of rows
return self.Contacts.count;
}
//單元格重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
//獲取集合內的元素添加到主標題上
cell.textLabel.text=self.Contacts[indexPath.row];
return cell;
}
//實現Cell自定義滑動操作
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//添加一個刪除按鈕
UITableViewRowAction *Top1=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
{
//更新數據
[self.Contacts removeObjectAtIndex:indexPath.row];
//更新 tableview
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//反饋執行 刪除操作
NSLog(@"刪除");
[tableView setEditing:NO animated:YES];
}];
//添加按鈕背景色
Top1.backgroundColor=[UIColor redColor];
//添加一個置頂按鈕
UITableViewRowAction *Top2=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置頂" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
{
//刷新數據
[self.Contacts exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
//把所選項置頂
NSIndexPath *FirstIndexPath=[NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:FirstIndexPath];
//反饋執行置頂操作
NSLog(@"置頂");
[tableView setEditing:NO animated:YES];
}];
//設置按鈕的背景色
Top2.backgroundColor=[UIColor grayColor];
//返回我們所設置的按鈕 但是得以數組的形式返回
return @[Top1,Top2];
}