zoukankan      html  css  js  c++  java
  • iOS 侧滑按钮的实现

    #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];
        
    }
  • 相关阅读:
    20165319第五周java学习笔记
    20165319 20165335 结对学习创意照
    20165215 结对编程——四则运算第二周
    20165215 2017-2018-2 《Java程序设计》第八周学习总结
    2017-2018-2 20165215 实验二 Java面向对象程序设计
    20165215 结对编程——四则运算第一周
    20165215 2017-2018-2 《Java程序设计》第7周学习总结
    20165215 2017-2018-2 《Java程序设计》第6周学习总结
    20165215 实验一 Java开发环境的熟悉
    20165215 2017-2018-2 《Java程序设计》第5周学习总结
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5313370.html
Copyright © 2011-2022 走看看