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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。