自定义批量删除

作者: Z了个L | 来源:发表于2016-02-17 18:33 被阅读148次
    // ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    
    @end
    
    
    // ViewController.m
    #import "ViewController.h"
    #import "XMGWine.h"
    #import "XMGWineCell.h"
    #import "MJExtension.h"
    
    @interface ViewController () <UITableViewDataSource, UITableViewDelegate>
    
    /** 酒模型数组*/
    @property (nonatomic, strong) NSMutableArray *wines;
    
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
    @end
    
    @implementation ViewController
    
    #pragma mark - 懒加载数据
    - (NSMutableArray *)wines
    {
        if (_wines == nil) {
            _wines = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
        }
        return _wines;
    }
    
    NSString *ID = @"wine";
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.rowHeight = 100;
        // 注册方法
        [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGWineCell class]) bundle:nil] forCellReuseIdentifier:ID];
    }
    
    /**
     *  删除模型
     */
    - (IBAction)remove{
    
        // 可变数组
        NSMutableArray *deleteArray = [NSMutableArray array];
        // 保存要删除的模型数据
        for (XMGWine *wine in self.wines) {
            if (wine.isChecked) {
                [deleteArray addObject:wine];
            }
        }
        // 删除数据
        [self.wines removeObjectsInArray:deleteArray];
        // 刷新
        [self.tableView reloadData];
    
    }
    
    #pragma mark -UITableViewDataSource方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.wines.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 去缓存取
        XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 传递模型数据
        cell.wine = self.wines[indexPath.row];
        return cell;
    }
    
    #pragma mark - UITableViewDelegate方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 获取数据
        XMGWine *wine = self.wines[indexPath.row];
        // 取反
        wine.checked = !wine.isChecked;
        // 刷新
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    
    @end
    
    // 酒模型数据
    // XMGWine.h
    #import <Foundation/Foundation.h>
    
    @interface XMGWine : NSObject
    
    /** 图片*/
    @property (nonatomic, strong) NSString *image;
    
    /** 价钱*/
    @property (nonatomic, strong) NSString *money;
    
    /** 酒名*/
    @property (nonatomic, strong) NSString *name;
    
    /** 是否选中*/
    /** checked,默认是NO*/
    @property (nonatomic, assign, getter=isChecked) BOOL checked;
    @end
    
    
    // XMGWine.m
    #import "XMGWine.h"
    
    @implementation XMGWine
    
    @end
    
    
    // XMGWineCell.h
    #import <UIKit/UIKit.h>
    @class XMGWine;
    @interface XMGWineCell : UITableViewCell
    
    /** 酒模型*/
    @property (nonatomic, strong) XMGWine *wine;
    
    @end
    
    // XMGWineCell.m
    #import "XMGWineCell.h"
    #import "XMGWine.h"
    
    @interface XMGWineCell ()
    
    /** 图片*/
    @property (nonatomic, weak) IBOutlet UIImageView *iconImageView;
    /** 名称*/
    @property (nonatomic, weak) IBOutlet UILabel *nameLabel;
    /** 价格*/
    @property (nonatomic, weak) IBOutlet UILabel *moneyLabel;
    /** 勾选图片*/
    @property (nonatomic, weak) IBOutlet UIImageView *checkedImageView;
    
    @end
    
    @implementation XMGWineCell
    
    - (void)setWine:(XMGWine *)wine
    {
        _wine = wine;
        self.iconImageView.image = [UIImage imageNamed:wine.image];
        self.nameLabel.text = wine.name;
        self.moneyLabel.text = [NSString stringWithFormat:@"¥%@", wine.money];
        // 是否隐藏
        self.checkedImageView.hidden = !wine.isChecked;
    }
    
    @end
    
    

    XMGWineCell.xib图片:

    批量删除前效果图片:

    批量删除后效果图片:

    • 笔者认为最重要的思想是:cell会重用,要想不被cell重用影响而产生一些错误,要深刻领悟MVC架构思想,View要显示的数据是由Model来提供的,操作界面的变幻,最好是通过修改模型来间接影响界面的变幻,这才是王道

    相关文章

      网友评论

        本文标题:自定义批量删除

        本文链接:https://www.haomeiwen.com/subject/rddmkttx.html