zoukankan      html  css  js  c++  java
  • IOS tableView 滑动删除与排序功能

    //
    //  ViewController.m
    //  0429
    //
    //  Created by apple on 15/4/29.
    //  Copyright (c) 2015年 gense. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "ProductCategory.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        NSMutableArray * productCategoryList ;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
     
        //从配置文件中初始化商品类型信息
       [self initProudctCategory];
        
    }
    
    #pragma mark  从配置文件中初始化商品类型信息
    - (void) initProudctCategory
    {
        //读取参数文件
        NSString * paramPath = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
        NSArray * dataArr = [NSArray arrayWithContentsOfFile:paramPath];
    
        productCategoryList = [NSMutableArray arrayWithCapacity:10];
        
        //遍历plist文件
        [dataArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [productCategoryList addObject: [ProductCategory productCategoryWithName:obj[@"name"] andDesc:obj[@"desc"] icon:obj[@"icon"]]];
        }];
        
    }
    
    
    #pragma mark tableviewDeleage  总共有多少行记录
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [productCategoryList count];
    }
    
    #pragma mark 实例化每行cell
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString * cellIdentified  = @"productCategoryTableViewCell";
        
        //从缓存中加载可用的cell
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentified];
        
        if(cell  == nil) //从缓存在未拿到合适的cell
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentified];
            
        }
        
        //设置cell中的属性
        cell.textLabel.text = [productCategoryList[indexPath.row] name];
        cell.detailTextLabel.text =  [productCategoryList[indexPath.row] desc];
        
        cell.imageView.image =  [UIImage imageNamed:[productCategoryList[indexPath.row] icon]];
        
        if([productCategoryList[indexPath.row] isSelected])
        {
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        }
        else{
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
        
        return  cell;
    }
    
    
    #pragma mark 设置tableview每行的高度
    
    - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 50.0;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [productCategoryList[indexPath.row] setIsSelected: ![productCategoryList[indexPath.row] isSelected ]];
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    
    }
    
    
    
    #pragma  mark 滑动删除
    - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
            if(UITableViewCellEditingStyleDelete == editingStyle)
            {
                [productCategoryList removeObjectAtIndex:indexPath.row];
                
                //[_productCategoryTV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
                
                [_productCategoryTV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
            }
    }
    
    
    #pragma mark 拖动排序
    -(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
        ProductCategory * p = productCategoryList[sourceIndexPath.row];
        
        [productCategoryList removeObject:p];
        
        
        [productCategoryList insertObject:p atIndex:destinationIndexPath.row];
        
    }
    
    
    
    #pragma mark  删除选中的数据
    - (IBAction)trashItemClick:(id)sender
    {
    //    NSMutableArray * deleteArr = [NSMutableArray arrayWithCapacity:10];
    //    NSMutableArray * indexPathArr = [NSMutableArray arrayWithCapacity:10    ];
    //    
    //    [productCategoryList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    //        if([obj isSelected])
    //        {
    //            [deleteArr addObject:obj];
    //            [indexPathArr addObject:[NSIndexPath indexPathForItem:idx inSection:0]];
    //        }
    //    }];
    //    
    //    [productCategoryList removeObjectsInArray:deleteArr];
    //    
    //    //tableview reload
    //    [_productCategoryTV deleteRowsAtIndexPaths:indexPathArr withRowAnimation:UITableViewRowAnimationMiddle];
        _productCategoryTV.editing = !_productCategoryTV.isEditing;
        
    }
    @end
  • 相关阅读:
    转发:原本优秀的我们是怎样滑向平庸的
    阿里巴巴C++实习生相关招聘一则
    [置顶] 我希望在20岁时就知道的26条时间管理技巧
    提示用户进行版本更新并且发布通知监控版本下载情况
    树状数组求正序数与逆序数hdu Minimum Inversion Number
    通过js 实现简单验证码的 刷新 看不清 换一张
    配置 CACTI 监控 MySQL 数据库状态
    Win7下ADB不能识别设备的可能原因
    Java 开源博客——B3log Solo 0.6.0 正式版发布了!
    MyEclipse下Import没有Maven的处理办法
  • 原文地址:https://www.cnblogs.com/wangjuneng/p/4469669.html
Copyright © 2011-2022 走看看