iOS开发资源iOS进阶指南iOS Developer

自定义批量删除

2016-02-17  本文已影响148人  Z了个L
// 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图片:

批量删除前效果图片:

批量删除后效果图片:

上一篇下一篇

猜你喜欢

热点阅读