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
时间: 2024-08-06 16:31:52

IOS tableView 滑动删除与排序功能的相关文章

android 下拉刷新上拉加载更多,高仿ios左滑动删除item,解决了众多手势问题

一.前言 老规矩,别的不说,这demo是找了很相关知识集合而成的,可以说对我这种小白来说是绞尽脑汁!程序员讲的是无图无真相!现在大家一睹为快! 二.比较关键的还是scroller这个类的 package com.icq.slideview.view; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; i

ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子

http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除的功能.在Android上面实现比较麻烦.本例子中不仅实现了左滑删除功能.还实现了左滑赞.左滑分享.左滑收藏等功能.当然大家也可以根据自己项目的需求来修改功能.QQ和微信也实现了相同的功能.大家可以看看.先上程序运行的效果 怎么样.大家看了这个截图是不是很心动呀.而且在左滑的时候还配有简单的滑动动画呢.非

iOS UITableViewCell滑动删除

一般我们使用列表的形式展现数据就会用到UITableView.在熟练掌握了用UITableView展示数据以后,开发过程中可能会遇到需要删除数据的需求,我们想实现在一行数据上划动一下,然后出现一个删除按钮的效果,其实只需要实现UITableView的一些代理方法就可以了. 首先,我们初始化一个界面,以列表的形式展示:#pragma mark - 初始化UI- (void)initUI{   self.view.backgroundColor = RGB(242, 242, 247);   sel

ios tableview 滑动到底部

tableview滑动到底部,根据页面不同 可以有两种方法 第一种: 一般样式的tableview 没有头和尾的 #pragma mark - 滑到最底部 - (void)scrollTableToFoot:(BOOL)animated { NSInteger s = [self.tableView numberOfSections]; //有多少组 if (s<1) return; //无数据时不执行 要不会crash NSInteger r = [self.tableView numberO

iOS Tableview侧滑删除和移动cell的实现

慕课网上学习了tableview的使用,突然让我觉得iOS比android简单多了,可能是我的感觉吧.因为android实现list view侧拉删除,动态移动item过程还是稍微有点复杂的.但是iOS却只需要重写几个方法就可以实现了.我只能说iOS太神奇!我就跟着做了一下. 项目地址:Todo 看效果,UI还可以.先上storyboard结构图: navigate controller 实现一个导航栏.view controller 实现一个tableview,tableviewCell .

tableView滑动删除

//1.设置可以编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{     return YES; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {    retu

iOS tableView侧滑删除的第三方控件

(到我的文件中,下载"tableview中cell测滑删除的第三方控件"),使用方法如下: 在tableView中的.m中,设置cell的方法上,事例代码如下,其中,EaseConversationCell继承于LYSideslipCell: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIden

[Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子

https://yunpan.cn/cueUIQkRafQrH (提取码:7ec1) 关于这样类似的例子网上的代码很多,最近发现这个例子里的代码在开发中会遇到一系列的问题.比如ListView的OnItemClickListener中无法获取在AppAdapter中getView方法convertView setTag的数据,所以需要优化下. 原因是控件中的SwipeMenuAdapter对Listview中的条目进行了修改和再封装. 具体优化的思路为,将控件中的SwipeMenuAdapter

【源码】仿QQ5.0和微信的滑动删除聊天列表

仿QQ5.0和微信的滑动删除聊天列表 功能分类:特效 支持平台:Android 运行环境:Android 开发语言:Java 开发工具:Eclipse 源码大小:2.21MB 下载地址:http://t.cn/R7eluap 源码简介 滑动删除ListView的Itemdemo 源码运行截图