// 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; @end @implementation XMGDealsViewController - (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)switchEditing { // 进入编辑模式 // self.tableView.editing = YES; [self.tableView setEditing:!self.tableView.isEditing animated:YES]; } - (IBAction)add { // tableView里面需要显示新的cell数据,只需要操作模型数据 XMGDeal *deal = [[XMGDeal alloc] init]; deal.title = [NSString stringWithFormat:@"XX饭店大打折 %d折", arc4random_uniform(50)]; deal.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)]; deal.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)]; deal.icon = @"5ee372ff039073317a49af5442748071"; [self.deals insertObject:deal atIndex:0]; XMGDeal *deal2 = [[XMGDeal alloc] init]; deal2.title = [NSString stringWithFormat:@"YY饭店大打折 %d折", arc4random_uniform(50)]; deal2.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)]; deal2.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)]; deal2.icon = @"5ee372ff039073317a49af5442748071"; [self.deals insertObject:deal2 atIndex:0]; // 提醒tabelView,模型数据发生了变化,请重新识别,请重新向数据源索要数据 [self.tableView reloadData]; // 插入某些特定的行 // [self.tableView insertRowsAtIndexPaths:@[ //第多少行 第多少组 // [NSIndexPath indexPathForRow:0 inSection:0], // [NSIndexPath indexPathForRow:1 inSection:0] // ] withRowAnimation:UITableViewRowAnimationLeft]; } - (IBAction)remove { // 移除模型数据 [self.deals removeObjectAtIndex:0]; [self.deals removeObjectAtIndex:0]; [self.deals removeObjectAtIndex:0]; // 刷新表格 [self.tableView reloadData]; // [self.tableView deleteRowsAtIndexPaths:@[ // [NSIndexPath indexPathForRow:0 inSection:0], // [NSIndexPath indexPathForRow:1 inSection:0], // [NSIndexPath indexPathForRow:2 inSection:0] // ] withRowAnimation:UITableViewRowAnimationRight]; // 15个cell:13 // 15个模型:12
//模型数和cell数必须相同;
} - (IBAction)update { // XMGDealCell *cell = (XMGDealCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]]; // cell.priceLabel.text = @"¥999"; // // return; // 修改模型 XMGDeal *deal = self.deals[3]; deal.price = [NSString stringWithFormat:@"%d", 50 + arc4random_uniform(100)];; // 刷新表格 [self.tableView reloadData]; // [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle]; } #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]; return cell; } #pragma mark - TableView代理方法 /** * 只要实现这个方法,左划cell出现删除按钮的功能就有了 * 用户提交了添加(点击了添加按钮)删除(点击了删除按钮)操作时会调用 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // 点击了“删除” // 删除模型 [self.deals removeObjectAtIndex:indexPath.row]; // 刷新表格 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+ NSLog(@"+++++ %zd", indexPath.row); } } /** * 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮) */ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete; } @end
// XMGDealCell.h // 06-自定义等高cell01-storyboard #import <UIKit/UIKit.h> @class XMGDeal; @interface XMGDealCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *priceLabel; /** 团购模型数据 */ @property (nonatomic, strong) XMGDeal *deal; + (instancetype)cellWithTableView:(UITableView *)tableView; @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; @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
// 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
// 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