国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[iOS基礎控件-6.6]展示團購數據自定義TableViewCell

2019-11-14 19:48:16
字體:
來源:轉載
供稿:網友
A.需求
1.頭部廣告
2.自定義cell:含有圖片、名稱、購買數量、價格
3.使用xib設計自定義cell,自定義cell繼承自UITableViewCell
4.尾部“加載更多按鈕”,以及其被點擊之后的數據加載刷新、動畫效果
 
code source: https://github.com/hellovoidworld/GroupPurchase
 
Image(81) 
 
 
B.實現
1.使用MVC
M:團購Model
V:總View、cell的View(包含類和界面)
C:ViewController
 
2.分類管理代碼文件
Image(82)
 
3.尾部footerView “加載更多"功能
1     // 設置尾部控件2     self.tableView.tableFooterView = footerView;
 
Image(83)Image(84)Image(85)
 
footerView設置的按鈕自動寬度為tableView寬度,只能設置高度;x和y也不能設置。
要自定義按鈕的的,使用xib進行自定義footerView嵌套
(1)使用button, 設計一個“加載更多”按鈕
(2)加上等待圖標
 
—>如何利用xib封裝一個View:
  • 新建xib描述view的結構
    Image(86)

  • 新建一個繼承UIView的類(取決于xib根對象的class)
  • 新建類名,和xib保持一致
    @interface FooterRefreshView : UIView

  • 設置xib控件的class,連線
    Image(87)

  • 在自定義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 @optional10 - (void) footerRefreshViewClickedFooterRefreshButton:(FooterRefreshView *) footerRefreshView;11 12 @end13 14 @interface FooterRefreshView : UIView15 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中
Image(88)
 
(2)創建繼承自UITableViewCell的類
@interface GroupPurchaseCell : UITableViewCell
 
(3)創建xib文件描述cell的界面
Image(89)
 
(4)指定view的class,對控件進行連線
Image(90)
 
(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)設計界面
Image(91)
 
 
(2)創建class加載圖片數據
Image(92)
 
(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)添加頁碼和自動輪播器
Image(93)
 
 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澳门| 论坛| 南皮县| 都江堰市| 满洲里市| 武功县| 湖北省| 鹤庆县| 盘锦市| 株洲县| 克山县| 怀集县| 鄄城县| 丰原市| 丰镇市| 老河口市| 肃宁县| 微山县| 建德市| 重庆市| 吴堡县| 抚远县| 如皋市| 二连浩特市| 图木舒克市| 公安县| 延庆县| 玉林市| 肇州县| 巧家县| 宁化县| 射洪县| 呼伦贝尔市| 长葛市| 柳江县| 郁南县| 高阳县| 鹤庆县| 梁山县| 靖远县| 醴陵市|