源码-0205-02--表格数据的批量操作02

 

//  XMGDealsViewController.m
//  06-自定义等高cell01-storyboard
#import "XMGDealsViewController.h"
#import "XMGDeal.h"
#import "XMGDealCell.h"

@interface XMGDealsViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 所有的团购数据 */
@property (nonatomic, strong) NSMutableArray *deals;

/** 即将要删除的团购 */
@property (nonatomic, strong) NSMutableArray *deletedDeals;
@end

@implementation XMGDealsViewController

- (NSMutableArray *)deletedDeals
{
    if (!_deletedDeals) {
        _deletedDeals = [NSMutableArray array];
    }
    return _deletedDeals;
}

- (NSMutableArray *)deals
{
    if (_deals == nil) {
        // 加载plist中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 字典数组 -> 模型数组
        NSMutableArray *dealArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            XMGDeal *deal = [XMGDeal dealWithDict:dict];
            [dealArray addObject:deal];
        }
        
        _deals = dealArray;
    }
    return _deals;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)remove {
    // 删除模型数据
    [self.deals removeObjectsInArray:self.deletedDeals];

    
    // 刷新表格
    [self.tableView reloadData];
    
    // 清空数组
    [self.deletedDeals removeAllObjects];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.deals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    // 创建cell
    XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];
    
    // 取出模型数据
    cell.deal = self.deals[indexPath.row];
    
    cell.checkView.hidden = ![self.deletedDeals containsObject:cell.deal];
    
    return cell;
}

#pragma mark - TableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中这一行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 取出模型
    XMGDeal *deal = self.deals[indexPath.row];
    if ([self.deletedDeals containsObject:deal]) {
        [self.deletedDeals removeObject:deal];
    } else {
        [self.deletedDeals addObject:deal];
    }
    
    // 刷新表格
    [tableView reloadData];
}
@end

 

 

//  XMGDealCell.m
//  06-自定义等高cell01-storyboard
#import "XMGDealCell.h"
#import "XMGDeal.h"

@interface XMGDealCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@end

@implementation XMGDealCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"deal";
    XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMGDealCell class]) owner:nil options:nil] lastObject];
    }
    return cell;
}

- (void)setDeal:(XMGDeal *)deal
{
    _deal = deal;
    
    // 设置数据
    self.iconView.image = [UIImage imageNamed:deal.icon];
    self.titleLabel.text = deal.title;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
}

@end
//  XMGDealCell.h
//  06-自定义等高cell01-storyboard
#import <UIKit/UIKit.h>
@class XMGDeal;

@interface XMGDealCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *checkView;
/** 团购模型数据 */
@property (nonatomic, strong) XMGDeal *deal;

+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end

 

//  XMGDeal.m
//  06-自定义等高cell01-storyboard
#import "XMGDeal.h"

@implementation XMGDeal
+ (instancetype)dealWithDict:(NSDictionary *)dict
{
    XMGDeal *deal = [[self alloc] init];
    
//    deal.title = dict[@"title"];
//    deal.icon = dict[@"icon"];
//    deal.buyCount = dict[@"buyCount"];
//    deal.price = dict[@"price"];
    
    // KVC - Key Value Coding
    [deal setValuesForKeysWithDictionary:dict];
    
    return deal;
}
@end
//  XMGDeal.h
//  06-自定义等高cell01-storyboard
#import <Foundation/Foundation.h>

@interface XMGDeal : NSObject
@property (strong, nonatomic) NSString *buyCount;
@property (strong, nonatomic) NSString *price;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *icon;

+ (instancetype)dealWithDict:(NSDictionary *)dict;
@end

 

posted @ 2017-02-27 12:44  laugh  阅读(196)  评论(0编辑  收藏  举报