IOS tableView 滑动删除与排序功能
2024-10-20 03:22:34
//
// 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
最新文章
- Ubuntu 12.4 Apache2 安装教程
- My安卓知识2--使用listview绑定sqlite中的数据
- 剑指offer系列39-----矩阵中的路径
- 1.C#基础篇-->;封装、继承和多态
- 【笨嘴拙舌WINDOWS】BMP图片浏览器
- A Bit Fun
- 《Android源代码设计模式解析与实战》读书笔记(十四)
- POJ 1741 Tree(树的点分治,入门题)
- 【Unity3D】Unity3D 让角色的头部望向鼠标
- .NET面试常考算法
- spring-data-jpa——如果使用了one-to-many,many-to-one的注解,在Jackson进行json字符串化时出现错误的解决方案
- gentoo emacs 中文输入法 呼出
- CentOS7 下安装mysql历程
- CDH的安装和设置
- 虚函数_构造函数_测试_VS2010x86
- 字符串拼接和dom回流
- WebForm——浏览器兼容、旋转、缩放、倾斜、移动
- 高版本SQL备份在低版本SQL还原问题
- Sublime Text 3 配置Python3.x
- springboot整合mybatis增删改查(四):完善增删改查及整合swgger2