TableView的数据记录实现
TableView的应用,真是有许多要讲的了,在我们做项目过程中,很多都用到TableVie,下面就讲到tableView实现商品购买,实现数据记录的方法。先看效果图。
单元格的ib文件
// // ViewController.h // TableView数据记录 // // Created by 小黑 on 15-10-21. // Copyright (c) 2015年 Mac. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
// // ViewController.m // TableView数据记录 // // Created by 小黑 on 15-10-21. // Copyright (c) 2015年 Mac. All rights reserved. // #import "ViewController.h" #import "NewTableView.h" @interface ViewController () //tableview的数据 @property (nonatomic,strong)NSMutableArray *dataArray; @property (nonatomic,strong)NewTableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _dataArray = [[NSMutableArray alloc] init]; for (int i = 0; i < 100; i++) { NSString *str = @"aa"; [_dataArray addObject:str]; } _tableView = [[NewTableView alloc] initWithFrame:[UIScreen mainScreen].bounds]; _tableView.dataArray = _dataArray; [self.view addSubview:_tableView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
// // NewTableView.h // TableView数据记录 // // Created by Mac on 15-10-23. // Copyright (c) 2015年 Mac. All rights reserved. // #import <UIKit/UIKit.h> @interface NewTableView : UITableView<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,copy)NSArray *dataArray; //记录cell上得数据 @property (nonatomic,strong)NSMutableArray *cellArray; @end
// // NewTableView.m // TableView数据记录 // // Created by Mac on 15-10-23. // Copyright (c) 2015年 Mac. All rights reserved. // #import "NewTableView.h" #import "NEwTableViewCell.h" @implementation NewTableView - (id)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { } return self; } - (void)awakeFromNib{ [super awakeFromNib]; } - (void)setDataArray:(NSArray *)dataArray{ _dataArray = dataArray; _cellArray = [[NSMutableArray alloc] init]; for (int i = 0; i< _dataArray.count; i++) { NSNumber *nab = [NSNumber numberWithInt:0]; NSString *num = [NSString stringWithFormat:@"%@",nab]; [_cellArray addObject:num]; } self.backgroundColor = [UIColor brownColor]; self.delegate = self; self.dataSource = self; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Identifir = @"cell"; NEwTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifir]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"NEwTableViewCell" owner:self options:nil] lastObject]; } cell.textF.text = _cellArray[indexPath.row]; if ([cell.textF.text intValue] == 0) { cell.delet.hidden = YES; }else{ cell.delet.hidden = NO; } [cell setTextFieldBlock:^(NSString *num) { if ([num intValue] <= 0) { int numInt = 0; num = [NSString stringWithFormat:@"%d",numInt]; } _cellArray[indexPath.row] = num; NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; [self reloadRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; }]; [cell setAddBlock:^(NSString *num) { _cellArray[indexPath.row] = num; NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; [self reloadRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; }]; [cell setDeletBlock:^(NSString *num) { _cellArray[indexPath.row] = num; NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; [self reloadRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; }]; cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44; } @end
// // NEwTableViewCell.h // TableView数据记录 // // Created by 小黑 on 15-10-21. // Copyright (c) 2015年 Mac. All rights reserved. // #import <UIKit/UIKit.h> @interface NEwTableViewCell : UITableViewCell<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *textF; @property (weak, nonatomic) IBOutlet UIButton *add; @property (weak, nonatomic) IBOutlet UIButton *delet; @property (copy,nonatomic)void(^addBlock)(NSString *num); @property (copy,nonatomic)void(^deletBlock)(NSString *num); @property (copy,nonatomic)void(^textFieldBlock)(NSString *num); @end
// // NEwTableViewCell.m // TableView数据记录 // // Created by 小黑 on 15-10-21. // Copyright (c) 2015年 Mac. All rights reserved. // #import "NEwTableViewCell.h" @implementation NEwTableViewCell - (IBAction)addButton:(UIButton *)sender { int num = [_textF.text intValue]; NSString *numString = [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:num+1]]; _addBlock(numString); } - (IBAction)delegtButton:(UIButton *)sender { int num = [_textF.text intValue]; int newNum = num - 1; if (newNum <= 0) { newNum = 0; } NSString *numString = [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:newNum]]; _deletBlock(numString); } - (void)textFieldDidBeginEditing:(UITextField *)textField{ if ([_textF.text intValue] == 0) { _textF.text = nil; } } - (void)textFieldDidEndEditing:(UITextField *)textField{ _textFieldBlock(textField.text); } //点击空白区域收起键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [_textF resignFirstResponder]; } //点击return收起键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [_textF resignFirstResponder]; return YES; } - (void)awakeFromNib { _textF.delegate = self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end