• UITableView的移动


      1 #import "RootViewController.h"
      2 #import "RootView.h"
      3 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
      4 @property (nonatomic, strong) RootView *rootView;
      5 // 定义大数组存放所有学生
      6 @property (nonatomic, strong) NSMutableArray *allDataArray;
      7 @end
      8 
      9 @implementation RootViewController
     10 
     11 - (void)loadView
     12 {
     13     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
     14     self.view = self.rootView;
     15 }
     16 
     17 - (void)viewDidLoad {
     18     [super viewDidLoad];
     19     // Do any additional setup after loading the view.
     20     self.rootView.tableView.dataSource = self;// 处理数据源
     21     
     22     self.rootView.tableView.delegate = self;// 处理视图
     23     // 设置数据
     24     [self handleData];
     25     // 处理NaviGation
     26     self.title = @"移动";
     27     self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
     28     self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
     29 // 添加右按钮
     30     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(rightBarButtonItemClick:)];
     31     
     32 }
     33 
     34 // 设置数据
     35 - (void)handleData
     36 {
     37     //1.初始化大数组
     38      self.allDataArray = [NSMutableArray array];
     39     //2. 定义三个数组分别存放三组学生姓名
     40     NSMutableArray *array1 = @[@"小明", @"小py", @"大肘子", @"张飞", @"根哥"].mutableCopy;
     41     NSMutableArray *array2 = @[@"高杰", @"宝月", @"周灿", @"傲然"].mutableCopy;
     42     NSMutableArray *array3 = @[@"刘洋", @"王志婷", @"小强", @"王志准", @"书凯", @"尹浩"].mutableCopy;
     43     // 添加到大数组中
     44     [self.allDataArray addObject:array1];
     45     [self.allDataArray addObject:array2];
     46     [self.allDataArray addObject:array3];
     47     
     48 }
     49 
     50 // 设置分区个数
     51 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     52 {
     53     return self.allDataArray.count;
     54 }
     55 
     56 // 设置每个分区的行数
     57 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     58 {
     59     return [self.allDataArray[section] count];
     60 }
     61 
     62 // 返回cell对象
     63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     64 {
     65     static NSString *identifier = @"cell";
     66     // 1.从重用队列查找可用的cell
     67     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     68     // 2,判断如果没有可重用的cell,创建cell对象
     69     if (!cell) {
     70         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
     71         
     72     }
     73     // 3. 设置数据
     74     cell.textLabel.text = [self.allDataArray[indexPath.section] objectAtIndex:indexPath.row];
     75     
     76     // 4. 返回cell对象
     77     return cell;
     78     
     79     
     80 }
     81 
     82 /**
     83  *  设置每一行的高度
     84  */
     85 
     86 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     87 {
     88     return 70;
     89 }
     90 
     91 #pragma mark UItableView 移动-----
     92 // 实现右按钮方法
     93 - (void)rightBarButtonItemClick:(UIBarButtonItem *)sender
     94 {
     95     //第一步: 让tableView处于编辑状态
     96     [self.rootView.tableView setEditing:!self.rootView.tableView.editing animated:YES];
     97     
     98 }
     99 
    100 // 第二步: 设置哪些cell可以移动
    101 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    102 {
    103     return YES;// 所有的cell都能一动
    104 }
    105 
    106 // 第三步: 开始移动
    107 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    108 {
    109     
    110      // 1.获取需要修改的数据
    111     NSString *sourceName = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
    112     
    113     // 2. 先将数据从当前位置移除
    114     [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    115     
    116     // 3. 将数据插入到对应位置
    117     [self.allDataArray[destinationIndexPath.section] insertObject:sourceName atIndex:destinationIndexPath.row];
    118 }
    119 
    120 #pragma mark 防止随意移动
    121 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
    122 {
    123     if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
    124         return proposedDestinationIndexPath;
    125     } else {
    126         return sourceIndexPath;
    127     }
    128 }
  • 相关阅读:
    Winform使用ML.NET时无法加载 DLL“CpuMathNative”问题的解决方法
    离线安装nuget包EPPlus
    码云上webide怎么提交
    EXCEL中自定义格式输入的数据怎么完整复制
    远程桌面剪贴板不好用了
    电脑里明明安装了net4.7但是VS里不显示?
    微信公众号里的音频怎么下载
    Hibernate-ORM:06.Hibernate中三种状态
    Hibernate-ORM:05.Hibernate中的list()和iterator()
    Hibernate-ORM:04.Hibernate中的get()和load()
  • 原文地址:https://www.cnblogs.com/leikun1113/p/5541110.html
Copyright © 2020-2023  润新知