zoukankan      html  css  js  c++  java
  • UItableView的编辑--删除移动cell

    //
    //  RootViewController.m
    //  UI__TableView的编辑
    //
    //  Created by dllo on 16/3/17.
    //  Copyright © 2016年 dllo. All rights reserved.
    //
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    <
        UITableViewDataSource,
        UITableViewDelegate
    >
    
    @property (nonatomic, retain) UITableView *tableView;
    @property (nonatomic, retain) NSMutableArray *arr;
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor cyanColor];
        self.navigationController.navigationBar.translucent = NO;
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
        [self.view addSubview:self.tableView];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [_tableView release];
        
        // 注册
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        //tableView 的编辑
        //右上角添加一个编辑按钮
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        //开启编辑模式
        //[self.tableView setEditing:YES animated:YES];
      
        //把tableView往下推200
        self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, 200)];
        imageView.image = [UIImage imageNamed:@"05.jpg"];
        imageView.tag = 1001;
        //将图片加到tableView的header
        //self.tableView.tableHeaderView = imageView;
        [self.tableView addSubview:imageView];
        [imageView release];
        //打印结构体
        NSLog(@"%@", NSStringFromCGRect(self.view.frame));
    }
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        //scrollView这儿是TableView
        NSLog(@"%@", [scrollView class]);
        // 以后在页面里同时存在tableView和scrollView时. 要事先判断当前那个控件触发的协议方法, 避免因为没有判断造成错误的操作
        //图片下拉变形
        NSLog(@"%f", scrollView.contentOffset.y);
        CGFloat yOffset = self.tableView.contentOffset.y;
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1001];
        if (yOffset < 0) {
            imageView.frame = CGRectMake(0, yOffset, self.view.frame.size.width, -yOffset);
        }
    }
    //重写系统的编辑按钮的点击方法
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
        [super setEditing:editing animated:animated];
        [self.tableView setEditing:editing animated:animated];
    }
    #pragma mark -- 设置那些行可以编辑, 通过返回yes和no来判断
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    #pragma mark -- 设置编辑的 样式, 默认是删除, 还有添加和多选 insert | delete(多选)
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return  UITableViewCellEditingStyleDelete;
    }
    // 状态只有是delete的时候可以左划出现删除
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"delete");
        // 根据不同的判断实现不同的效果
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //要实现删除, 第一件事就是删除对应的数组里面的数据
            [self.arr removeObjectAtIndex:indexPath.row];
          //  [self.tableView reloadData];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        }
    }
    //改变红色区域的文字
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
        return @"123";
        
    }
    // 改变划出来的区域的个数和颜色和文字
    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle:0 title:@"你猜" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        }];
        firstAction.backgroundColor = [UIColor yellowColor];
        //第二个rowAction
        UITableViewRowAction *secAction = [UITableViewRowAction rowActionWithStyle:0 title:@"我才不猜" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            //在block里面, 写对应的点击事件
            NSLog(@"2222");
        }];
        return @[firstAction, secAction];
    }
    // 移动
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        // 1. 现获取要移动的数据
        NSString *str = [self.arr[sourceIndexPath.row] retain];
        // 2. 把数组里的相应的字符串除掉
        [self.arr removeObjectAtIndex:sourceIndexPath.row];
        // 3. 把新的字符串插入 到数组
        [self.arr insertObject:str atIndex:destinationIndexPath.row];
        [str release];
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        return self.arr.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       //注册 新方法
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
        cell.textLabel.text = self.arr[indexPath.row];
        return  cell;
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (void)dealloc {
        [_tableView release];
        [_arr release];
        [super dealloc];
    }
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end

    效果图:

  • 相关阅读:
    零散
    修改element的内部样式的两种方式
    在vue-cli项目中使用第三方的js,不是es6格式
    Docker知识
    golang使用grpc
    vue中axios导出文件
    nginx、vue和thinkphp配置
    Mysql的一些问题
    数据库索引失效原因
    golang中使用grpc服务
  • 原文地址:https://www.cnblogs.com/mafeng/p/5289046.html
Copyright © 2011-2022 走看看