IOS tableView 滑動刪除與排序功能

來源:互聯網
上載者:User

標籤:

////  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 滑動刪除與排序功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.