A.需求
1.頭部廣告
2.自定義cell:含有圖片、名稱、購買數量、價格
3.使用xib設計自定義cell,自定義cell繼承自UITableViewCell
4.尾部“加載更多按鈕”,以及其被點擊之后的數據加載刷新、動畫效果
code source: https://github.com/hellovoidworld/GroupPurchase
B.實現
1.使用MVC
M:團購Model
V:總View、cell的View(包含類和界面)
C:ViewController
2.分類管理代碼文件
3.尾部footerView “加載更多"功能
1 // 設置尾部控件 2 self.tableView.tableFooterView = footerView;
footerView設置的按鈕自動寬度為tableView寬度,只能設置高度;x和y也不能設置。
要自定義按鈕的的,使用xib進行自定義footerView嵌套
(1)使用button, 設計一個“加載更多”按鈕
(2)加上等待圖標
—>如何利用xib封裝一個View:
- 新建xib描述view的結構
- 新建一個繼承UIView的類(取決於xib根對象的class)
- 新建類名,和xib保持一致
@interface FooterRefreshView : UIView - 設置xib控件的class,連線
- 在自定義class中定義xib的加載類方法(屏蔽xib加載過程)
1 /** 初始化方法 */ 2 + (instancetype) footerRrefreshViewWithDelegate:(id<FooterRefreshViewDelegate>) delegate { 3 FooterRefreshView *footerRefreshView = [[[NSBundle mainBundle] loadNibNamed:@"FooterRefreshView" owner:nil options:nil] lastObject]; 4 5 if (nil != delegate) { 6 footerRefreshView.delegate = delegate; 7 } 8 9 return footerRefreshView; 10 }
- class持有controller引用,發送消息給controller刷新數據
下面使用代理模式
—>改進:使用代理設計模式
- 自定義view的class持有controller的引用,耦合性強 —>使用代理
- 協議命名規范:控件類名+Delegate
- 代理方法普遍都是@optional
- 代理對象遵守代理協議,實現代理協議里面的方法
- 在需要的地方調用代理方法,給代理發送消息
1 FooterRefreshView.h 2 #import <UIKit/UIKit.h> 3 4 @class FooterRefreshView; 5 6 // 定義delegate協議 7 @protocol FooterRefreshViewDelegate <NSObject> 8 9 @optional 10 - (void) footerRefreshViewClickedFooterRefreshButton:(FooterRefreshView *) footerRefreshView; 11 12 @end 13 14 @interface FooterRefreshView : UIView 15 16 + (instancetype) footerRrefreshViewWithDelegate:(id<FooterRefreshViewDelegate>) delegate; 17 18 @end
4.xib中創建的view初始化完畢之后不會調用class中的init方法,而是調用awakeFromNib方法
FooterRefreshView.h
1 // xib控件的初始化調用方法 2 - (void)awakeFromNib { 3 self.loadingImage.hidden = YES; 4 }
5.分割線
其實就是高度為1的UIView
6.自定義cell
(1).自定義cell的子控件都要放到contentView里面
默認就是會放到contentView中
(2)創建繼承自UITableViewCell的類
@interface GroupPurchaseCell : UITableViewCell
(3)創建xib文件描述cell的界面
(4)指定view的class,對控件進行連線
(5)在cell類中,定義一個model成員,用來存儲這個cell的數據
1 /** 自定初始化的類方法,傳入model數據 */ 2 + (instancetype) groupPurchaseCellWithGroupPurchase:(GroupPurchase *) groupPurchase;
(6)創建初始化方法,使用model數據作為傳入參數
1 /** 自定初始化的類方法,傳入model數據 */ 2 + (instancetype) groupPurchaseCellWithGroupPurchase:(GroupPurchase *) groupPurchase { 3 GroupPurchaseCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"GroupPurchaseCell" owner:nil options:nil] lastObject]; 4 5 // 加載model中的數據,初始化界面 6 cell.groupPurchase = groupPurchase; 7 8 return cell; 9 } 10 11 /** 沒有model數據的空cell */ 12 + (instancetype)groupPurchaseCell { 13 return [self groupPurchaseCellWithGroupPurchase:nil]; 14 }
(7)傳入model數據的同時,加載數據到view上面
1 /** 加載Model數據,初始化界面 */ 2 - (void) setGroupPurchase:(GroupPurchase *) groupPurchase { 3 if (nil != groupPurchase) { 4 self.titleLabel.text = groupPurchase.title; 5 self.iconImageView.image = [UIImage imageNamed:groupPurchase.icon]; 6 self.priceLabel.text = [NSString stringWithFormat:@"¥%@", groupPurchase.price]; 7 self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已經購買", groupPurchase.buyCount]; 8 } 9 10 _groupPurchase = groupPurchase; 11 }
7.頭部廣告
其實就是之前的滾動廣告放到 self.tableView.tableHeaderView
(1)設計界面
(2)創建class加載圖片數據
(3)傳入圖片名的數組作為成員
1 // 廣告組 2 @property(nonatomic, strong) NSArray *ads;
(4)加載圖片
1 /** 設置ads */ 2 - (void) setAds:(NSArray *)ads { 3 if (nil != ads) { 4 CGFloat adImageWidth = AD_VIEW_WIDTH; 5 CGFloat adImageHeight = AD_VIEW_HEIGHT; 6 CGFloat adImageY = 0; 7 8 for (int i=0; i<ads.count; i++) { 9 // 計算當前圖片的水平坐標 10 CGFloat adImageX = i * adImageWidth; 11 12 UIImageView *adImageView = [[UIImageView alloc] initWithFrame:CGRectMake(adImageX, adImageY, adImageWidth, adImageHeight)]; 13 adImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", ads[i]]]; 14 15 [self.scrollView addSubview:adImageView]; 16 } 17 18 // 設置滾動 19 self.scrollView.contentSize = CGSizeMake(ads.count * AD_VIEW_WIDTH, 0); 20 self.scrollView.scrollEnabled = YES; 21 } 22 23 _ads = ads; 24 }
(5)在主controller,設置好圖片數據,創建頭部控件加到頭部位置
1 //設置頭部廣告 2 HeaderAdView *adView = [self genAdView]; // 手動拼裝廣告圖片數據 3 self.tableView.tableHeaderView = adView;
(6)添加頁碼和自動輪播器