zoukankan      html  css  js  c++  java
  • UITableViewDelete 删除

    #import "ViewController.h"

    #import "FWZViewController.h"

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

    {

        NSMutableArray *_arrayM;

        UITableView *_tableView;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        //编辑模式

        self.navigationItem.leftBarButtonItem = self.editButtonItem;

        [self getData];

        [self addUITableView];

    }

    - (void)getData{

        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

        NSArray *array = [userDefault objectForKey:@"Array"];

        if (array == nil) {

            _arrayM = [NSMutableArray array];

            for (int i = 0; i < 10; i ++) {

                NSString *str = [NSString stringWithFormat:@"我是第%d行",i];

                [_arrayM addObject:str];

                

            }

        }else{

            _arrayM = [NSMutableArray arrayWithArray:array];

        }

        }

    - (void)addUITableView{

        _tableView  = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

        _tableView.delegate = self;

        _tableView.dataSource = self;

        [self.view addSubview:_tableView];

    }

    #pragma mark - UITableViewDataSource

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return _arrayM.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        NSString *resuID = @"ID";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuID];

        if (cell == nil) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resuID];

        }

        cell.textLabel.text = _arrayM[indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        return cell;

    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //1.进入下一页,并传值过去

        FWZViewController *fwz = [[FWZViewController alloc]init];

        fwz.cellName = _arrayM[indexPath.row];

        [self.navigationController pushViewController:fwz animated:YES];

    }

    #pragma mark - 点击编辑按钮触发事件

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated{

        //1.重写按钮编辑点击事件  首先要调用父类方法

        [super setEditing:editing animated:YES];

        //2.打开UItableview的编辑模式

        [_tableView setEditing:editing animated:YES];

        

    }   //3.允许row编辑

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

        return YES;

    }

        //4.设置编辑样式

    - (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    //    UITableViewCellEditingStyleNone,

    //    UITableViewCellEditingStyleDelete,

    //    UITableViewCellEditingStyleInsert

        return UITableViewCellEditingStyleDelete;

    }

        //5.提交编辑效果

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

        if (editingStyle ==  UITableViewCellEditingStyleDelete) {

            // 从数据源删除数据

            [_arrayM removeObjectAtIndex:indexPath.row];

            [self userDefault];

            //提交并刷新

            [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

            

            

        }

    }

    #pragma mark - 编辑状态下可以移动cell

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

        

        NSLog(@"%ld %ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);

         NSLog(@"%ld %ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);

        // 取出目标数据

        NSString *str  = [_arrayM objectAtIndex:sourceIndexPath.row];

        //删除目标数据

        [_arrayM removeObjectAtIndex:sourceIndexPath.row];

        

        [_arrayM insertObject:str atIndex:destinationIndexPath.row];

        [self userDefault];

    }

    - (void)userDefault{

        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

        [userDefault setObject:_arrayM forKey:@"Array"];

        NSLog(@"%@",_arrayM);

    }

    FWZViewController.h

    #import <UIKit/UIKit.h>

    @interface FWZViewController : UIViewController

    @property (nonatomic,assign)NSString *cellName;

    @end

    FWZViewController.m

    #import "FWZViewController.h"

    #define SCREEN_W self.view.frame.size.width

    @interface FWZViewController ()

    {

        UILabel *_label;

    }

    @end

    @implementation FWZViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        [self addUIlabel];

    }

    - (void)addUIlabel{

        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_W,100)];

        _label.backgroundColor = [UIColor purpleColor];

        _label.text = self.cellName;

        _label.textColor = [UIColor blackColor];

        [self.view addSubview:_label];

    }

  • 相关阅读:
    大型站点技术架构PDF阅读笔记(一):
    【大话QT之十三】系统软件自己主动部署实现方案
    VS编译duilib项目时候的错误解决方法整理
    Missing iOS Distribution signing identity for …, 在打包的时候发现证书过期了。
    Django项目国际化
    Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
    BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]
    NOIP2016DAY1题解
    清北学堂入学测试P4751 H’s problem(h)
    BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
  • 原文地址:https://www.cnblogs.com/fanwenzheIOS/p/4973856.html
Copyright © 2011-2022 走看看