[iOS基礎控制項,ios基礎控制項

來源:互聯網
上載者:User

[iOS基礎控制項,ios基礎控制項
A.需求1.頭部廣告2.自訂cell:含有圖片、名稱、購買數量、價格3.使用xib設計自訂cell,自訂cell繼承自UITableViewCell4.尾部“載入更多按鈕”,以及其被點擊之後的資料載入重新整理、動畫效果 1 // 設定尾部控制項2 self.tableView.tableFooterView = footerView;  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中@interface GroupPurchaseCell : UITableViewCell (3)建立xib檔案描述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)設計介面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)添加頁碼和自動輪播器   

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.