標籤:
//// 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
IOS tableView 滑動刪除與排序功能