标签:
一、Model
1 #import <Foundation/Foundation.h> 2 3 @interface Goods : NSObject 4 5 @property (nonatomic, copy) NSString *icon; 6 @property (nonatomic, copy) NSString *title; 7 @property (nonatomic, copy) NSString *price; 8 @property (nonatomic, copy) NSString *buyCount; 9 10 - (instancetype) initWithDict:(NSDictionary *)dict; 11 + (instancetype) goodsWithDict:(NSDictionary *)dict; 12 13 14 @end 15 16 #import "Goods.h" 17 18 @implementation Goods 19 20 - (instancetype)initWithDict:(NSDictionary *)dict 21 { 22 if (self = [super init]) { 23 [self setValuesForKeysWithDictionary:dict]; 24 } 25 return self; 26 } 27 28 + (instancetype)goodsWithDict:(NSDictionary *)dict 29 { 30 return [[self alloc] initWithDict:dict]; 31 } 32 33 @end
二、View
1 #import <UIKit/UIKit.h> 2 3 @interface BWHeaderView : UIView 4 5 + (instancetype)headerView; 6 7 @end 8 9 #import "BWHeaderView.h" 10 11 @interface BWHeaderView () 12 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; 13 14 @end 15 16 @implementation BWHeaderView 17 18 //当这个方法被执行的时候就表示BWHeaderView已经从xib文件中创建好了 19 //BWHeaderView的子控件也都创建好了,所以就可以使用UIScrollView了。 20 - (void)awakeFromNib 21 { 22 23 } 24 25 //创建headerView 26 + (instancetype)headerView 27 { 28 BWHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"BWHeaderView" owner:nil options:nil] lastObject]; 29 return headerView; 30 } 31 32 @end
1 #import <UIKit/UIKit.h> 2 3 // 协议命名规范: 4 // 类名 + Delegate 5 // 协议中的方法最好加@optional 6 // 定义一个delegate属性,delegate属性用weak 7 // delegate属性声明为id类型,可以用来解除对头文件的依赖 8 9 @class BWFooterView; 10 @protocol BWFooterViewDelegate <NSObject> 11 12 @required 13 - (void)footerViewUpDateData:(BWFooterView *)footerView; 14 @end 15 16 @interface BWFooterView : UIView 17 18 @property(weak, nonatomic) id<BWFooterViewDelegate> delegate; 19 20 + (instancetype)footerView; 21 22 @end 23 24 #import "BWFooterView.h" 25 26 @interface BWFooterView () 27 @property (weak, nonatomic) IBOutlet UIButton *btnLoadMore; 28 @property (weak, nonatomic) IBOutlet UIView *waittingView; 29 - (IBAction)btnLoadMoreClick:(id)sender; 30 31 @end 32 33 @implementation BWFooterView 34 35 //通过xib设置tableView中的tableFooterView 36 + (instancetype)footerView 37 { 38 BWFooterView *footerView = [[[NSBundle mainBundle] loadNibNamed:@"BWFooterView" owner:nil options:nil] lastObject]; 39 return footerView; 40 } 41 42 //加载按钮单击事件 43 - (IBAction)btnLoadMoreClick:(id)sender { 44 45 //1.隐藏“加载更多”按钮 46 self.btnLoadMore.hidden = YES; 47 //2.显示“等待指示器”所在的那个View 48 self.waittingView.hidden = NO; 49 50 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 51 //调用代理方法 52 if ([self.delegate respondsToSelector:@selector(footerViewUpDateData:)]) { 53 //3.增加一条数据 54 //3.1创建一个模型对象 55 //3.2把模型对象加载控制器的goods集合中 56 //4.刷新UITableView 57 [self.delegate footerViewUpDateData:self]; 58 } 59 //5.显示“加载更多”按钮 60 self.btnLoadMore.hidden = NO; 61 //6.隐藏“等待指示器”所在的那个View 62 self.waittingView.hidden = YES; 63 }); 64 65 } 66 @end
1 #import <UIKit/UIKit.h> 2 @class Goods; 3 4 @interface BWGoodsCell : UITableViewCell 5 6 @property (weak, nonatomic) IBOutlet UIImageView *imgView; 7 @property (weak, nonatomic) IBOutlet UILabel *lblName; 8 @property (weak, nonatomic) IBOutlet UILabel *lblPrice; 9 @property (weak, nonatomic) IBOutlet UILabel *lblBuyCount; 10 11 @property (strong, nonatomic) Goods *myGoods; 12 13 + (instancetype)goodsCellWithTableView:(UITableView *)tableView; 14 @end 15 16 17 #import "BWGoodsCell.h" 18 #import "Goods.h" 19 20 @interface BWGoodsCell () 21 22 @end 23 24 @implementation BWGoodsCell 25 26 // 重写setMyGoods方法,把模型的数据设置给子控件 27 - (void)setMyGoods:(Goods *)myGoods 28 { 29 _myGoods = myGoods; 30 31 self.imgView.image = [UIImage imageNamed:_myGoods.icon]; 32 self.lblName.text = _myGoods.title; 33 self.lblPrice.text = [NSString stringWithFormat:@"$ %@",_myGoods.price]; 34 self.lblBuyCount.text = [NSString stringWithFormat:@"%@个人已购买",_myGoods.buyCount]; 35 } 36 37 // 创建单元格 38 + (instancetype)goodsCellWithTableView:(UITableView *)tableView 39 { 40 static NSString *cellIndentifier = @"cellIndentifier"; 41 BWGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier]; 42 43 if (!cell) { 44 cell = [[[NSBundle mainBundle] loadNibNamed:@"BWGoodsCell" owner:nil options:nil] firstObject]; 45 } 46 return cell; 47 } 48 - (void)awakeFromNib { 49 // Initialization code 50 } 51 52 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 53 [super setSelected:selected animated:animated]; 54 55 // Configure the view for the selected state 56 } 57 58 @end
三、Controller
1 #import "ViewController.h" 2 #import "Goods.h" 3 #import "BWGoodsCell.h" 4 #import "BWFooterView.h" 5 #import "BWHeaderView.h" 6 7 @interface ViewController ()<UITableViewDataSource,BWFooterViewDelegate> 8 9 @property (nonatomic, strong) NSMutableArray *arrayModel; 10 @property (nonatomic, strong) UITableView *tableView; 11 12 @end 13 14 @implementation ViewController 15 16 #pragma mark - 懒加载 17 - (NSArray *)arrayModel 18 { 19 if (_arrayModel == nil) { 20 NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]; 21 22 NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path]; 23 24 NSMutableArray *arrayModel = [NSMutableArray array]; 25 26 for (NSDictionary *dict in arrayDict) { 27 Goods *goodsModel = [Goods goodsWithDict:dict]; 28 [arrayModel addObject:goodsModel]; 29 } 30 _arrayModel = arrayModel; 31 } 32 33 return _arrayModel; 34 } 35 36 #pragma mark - 加载视图 37 - (void)viewDidLoad { 38 [super viewDidLoad]; 39 self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped]; 40 self.tableView.dataSource =self; 41 42 [self.view addSubview:self.tableView]; 43 self.tableView.rowHeight = 100; 44 45 //ps:tableView 的tableFooterView特点:只能修改x和height值,y和height不能修改 46 47 //创建tableFooterView 48 BWFooterView *footerView = [BWFooterView footerView]; 49 //设置footerView的代理 50 footerView.delegate =self; 51 self.tableView.tableFooterView = footerView; 52 53 //创建tableHeaderView 54 BWHeaderView *headerView = [BWHeaderView headerView]; 55 56 self.tableView.tableHeaderView = headerView; 57 58 59 } 60 #pragma mark - CZFooterView的代理方法 61 - (void)footerViewUpDateData:(BWFooterView *)footerView 62 { 63 //3.增加一条数据 64 //3.1创建一个模型对象 65 Goods *model = [[Goods alloc] init]; 66 model.title = @"驴肉火烧"; 67 model.price = @"6.0"; 68 model.buyCount = @"1000"; 69 model.icon = @"7003217f16ed29bab85e635a3bd6b60d"; 70 //3.2把模型对象加载控制器的goods集合中 71 [self.arrayModel addObject:model]; 72 //4.刷新UITableView 73 [self.tableView reloadData]; 74 75 //ps:局部刷新(仅适用于UITableView的总行数没有发生变化的时候) 76 // NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.arrayModel.count-1 inSection:0]; 77 // [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationLeft]; 78 79 //5.把UITableView中最后一行滚动到最上面 80 NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.arrayModel.count-1 inSection:0]; 81 [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 82 } 83 84 #pragma mark - 数据源 85 //加载组的行数 86 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 87 { 88 return self.arrayModel.count; 89 } 90 //加载单元格数据 91 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 92 { 93 //1.获取数据模型 94 Goods *goodsModel = self.arrayModel[indexPath.row]; 95 96 //2.创建单元格 97 BWGoodsCell *cell = [BWGoodsCell goodsCellWithTableView:tableView]; 98 99 // 在控制器中直接为cell的每个子控件赋值数据造成问题 100 // 1>控制器强依赖于cell,一旦cell内部的子控件发生变化,那么子控件中的代码也得改(紧耦合) 101 // 2>cell封装不够完整,凡是用到cell的地方 102 // 3>解决:直接把模型传递给自定义cell,然后在自定义cell内部解析model中的数据赋值给自定义cell的内部的子控件 103 104 //3.把模型数据设置给单元格 105 cell.myGoods = goodsModel; 106 107 //4.返回单元格 108 return cell; 109 } 110 111 #pragma mark - 状态栏 112 - (BOOL)prefersStatusBarHidden 113 { 114 return YES; 115 } 116 117 #pragma mark - 内存 118 - (void)didReceiveMemoryWarning { 119 [super didReceiveMemoryWarning]; 120 // Dispose of any resources that can be recreated. 121 } 122 123 @end
iOS UI-团购案例(通过xib文件自定义UITableViewCell)
标签:
原文地址:http://www.cnblogs.com/oc-bowen/p/5111213.html