自定義批量刪除

// 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>

/** 酒模型數(shù)組*/
@property (nonatomic, strong) NSMutableArray *wines;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

#pragma mark - 懶加載數(shù)據(jù)
- (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{

    // 可變數(shù)組
    NSMutableArray *deleteArray = [NSMutableArray array];
    // 保存要刪除的模型數(shù)據(jù)
    for (XMGWine *wine in self.wines) {
        if (wine.isChecked) {
            [deleteArray addObject:wine];
        }
    }
    // 刪除數(shù)據(jù)
    [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];

    // 傳遞模型數(shù)據(jù)
    cell.wine = self.wines[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 獲取數(shù)據(jù)
    XMGWine *wine = self.wines[indexPath.row];
    // 取反
    wine.checked = !wine.isChecked;
    // 刷新
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

@end

// 酒模型數(shù)據(jù)
// 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重用影響而產(chǎn)生一些錯誤,要深刻領悟MVC架構思想,View要顯示的數(shù)據(jù)是由Model來提供的,操作界面的變幻,最好是通過修改模型來間接影響界面的變幻,這才是王道
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容