麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

IOSUITableView多選刪除功能

2019-11-14 18:11:30
字體:
來源:轉載
供稿:網友

  UITbableView作為列表展示信息,除了展示的功能,有時還會用到刪除,比如購物車、收藏列表等。

  單行刪除功能可以直接使用系統自帶的刪除功能,當橫向輕掃cell時,右側出現紅色的刪除按鈕,點擊刪除當前cell。或者讓表格進入編輯狀態后,點擊左側的紅色按鈕,右側出現刪除按鈕,刪除,如下圖所示。單行自帶刪除已經在前面文章中進行過講解,需要的可以去查閱。

  多選刪除是點擊編輯按鈕,讓表格進入編輯狀態后,每行的左側出現一個小圓圈,當點擊行的時候,可以選中該行或者取消選中該行,當點擊按鈕確定刪除的時候才會把選中的行全部刪除掉,如圖所示。

使用系統多選刪除功能的步驟:

1、讓tableView進入編輯狀態,也就是設置它的editing為YES

2、返回編輯模式,也就是實現UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert。如果不實現,默認返回的就是刪除模式

3、實現UITableViewDelegate中的tableView: didSelectRowAtIndexPath: 和tableView: didDeselectRowAtIndexPath:方法。在里面對選中的商品集合中的數據進行修改

4、點擊刪除時,將選中商品數據從列表對應總商品集合中刪除掉,并刷新界面。

代碼:

//  Goods.h//  購物車表格刪除////  Created by jerei on 15-1-7.//  Copyright (c) 2015年 jerei. All rights reserved.//#import <Foundation/Foundation.h>@interface Goods : NSObject@PRoperty (nonatomic, copy) NSString *icon;@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *details;-(id)initWithDic:(NSDictionary*)dic;+(id)goodsWithDic:(NSDictionary*)dic;@end////  Goods.m//  購物車表格刪除////  Created by jerei on 15-1-7.//  Copyright (c) 2015年 jerei. All rights reserved.//#import "Goods.h"@implementation Goods-(id)initWithDic:(NSDictionary *)dic{    if (self = [super init]) {        self.icon = [dic objectForKey:@"icon"];        self.name = [dic objectForKey:@"name"];        self.details = [dic objectForKey:@"details"];    }    return self;}+(id)goodsWithDic:(NSDictionary *)dic{    Goods *good = [[Goods alloc] initWithDic:dic];    return good;}@end////  ViewController.m//  JRTableView多選刪除////  Created by jerehedu on 15/6/11.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#import "ViewController.h"#import "Goods.h"@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>{    UITableView *_tableView; //列表        NSMutableArray *_goodsAry; //商品數組        NSMutableArray *_selectArray; //選中的數組        UIButton *_editBtn; //編輯按鈕}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];            //初始化選中數組    _selectArray = [NSMutableArray array];        //設置界面    [self setTheInterface];        //取數據    [self getGoodsInfoFromFile];}#pragma mark - 取數據-(void)getGoodsInfoFromFile{    NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]];        //把數據存到模型對象中,然后把對象存到數組中    _goodsAry = [NSMutableArray array];    for (int i=0; i<ary.count; i++) {        Goods *good = [Goods goodsWithDic:ary[i]];        [_goodsAry addObject:good];    }}#pragma mark - 初始化界面-(void)setTheInterface{    //bg    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    imgView.image = [UIImage imageNamed:@"redup.png"];    [self.view addSubview:imgView];        //添加標題    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];    titleLabel.text = @"購物車";    titleLabel.textAlignment = NSTextAlignmentCenter;    titleLabel.textColor = [UIColor whiteColor];    [self.view addSubview:titleLabel];        //添加編輯按鈕    _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];    _editBtn.frame = CGRectMake(self.view.frame.size.width-60, 25, 50, 34);    [_editBtn setTitle:@"編輯" forState:UIControlStateNormal];    [_editBtn setTitle:@"刪除" forState:UIControlStateSelected];    _editBtn.titleLabel.font = [UIFont systemFontOfSize:15];    _editBtn.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];    [self.view addSubview:_editBtn];    [_editBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside];        //添加tableview    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];    _tableView.dataSource = self;    _tableView.delegate = self;    [self.view addSubview:_tableView];    _tableView.rowHeight = 110;}#pragma mark 數據源  返回有幾行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return _goodsAry.count;}#pragma mark 每行顯示內容-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *idGood = @"goods";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idGood];        if (cell==nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idGood];        cell.detailTextLabel.numberOfLines = 6;        cell.detailTextLabel.textColor = [UIColor brownColor];    }        Goods *good = _goodsAry[indexPath.row];        cell.imageView.image = [UIImage imageNamed:good.icon];    cell.textLabel.text = good.name;    cell.detailTextLabel.text = good.details;    return cell;}#pragma mark 選中行-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    if (!_tableView.editing)        return;        Goods *good = [_goodsAry objectAtIndex:indexPath.row];    if (![_selectArray containsObject:good]) {        [_selectArray addObject:good];    }}#pragma mark 取消選中行-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{    if (!_tableView.editing)        return;        Goods *good = [_goodsAry objectAtIndex:indexPath.row];    if ([_selectArray containsObject:good]) {        [_selectArray removeObject:good];    }}#pragma mark 點擊編輯按鈕- (IBAction)clickEditBtn:(UIButton *)sender {        BOOL flag = _tableView.editing;    if (flag) {        //刪除的操作        //得到刪除的商品索引        NSMutableArray *indexArray = [NSMutableArray array];        for (Goods *good in _selectArray) {            NSInteger num = [_goodsAry indexOfObject:good];                        NSIndexPath *path = [NSIndexPath indexPathForRow:num inSection:0];            [indexArray addObject:path];        }                //修改數據模型        [_goodsAry removeObjectsInArray:_selectArray];        [_selectArray removeAllObjects];                //刷新        [_tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];                _tableView.editing = NO;        _editBtn.selected = NO;    }else    {        //開始選擇行        [_selectArray removeAllObjects];                _tableView.editing = YES;        _editBtn.selected = YES;    }}#pragma mark 返回編輯模式,默認為刪除模式-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;}@end

 

作者:杰瑞教育
出處:http://www.companysz.com/jerehedu/ 
版權聲明:本文版權歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
技術咨詢:JRedu技術交流
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: av在线播放电影 | 成人在线观看免费 | 亚洲视屏在线观看 | 日韩av片网站| av在线播放免费观看 | 九九热视频在线免费观看 | 免费毛片a线观看 | 国产免费人做人爱午夜视频 | 免费毛片免费看 | av在线免费观看网 | 国产在线欧美日韩 | 成人性视频在线 | 毛片大全免费 | 午夜精品小视频 | 欧美一级aa免费毛片 | 国产午夜精品理论片a级探花 | 99ri在线| 国产精品久久久久久影视 | 成熟女人特级毛片www免费 | 久久成人国产精品 | 久久羞羞| 成人羞羞视频在线观看免费 | av在线成人 | 亚洲天堂字幕 | 久久艹国产精品 | 久久久婷婷 | 91在线视频在线观看 | 日韩黄网站 | 国产一级毛片高清 | 九九久久视频 | 久久亚洲国产精品 | 在线成人亚洲 | 青青国产在线视频 | 精品中文一区 | 日本中文字幕电影在线观看 | 在线2区 | 国产福利视频 | 国产精品久久久久久婷婷天堂 | 亚洲第一成人在线观看 | 久国久产久精永久网页 | 爽妇网国产精品 |