貓貓學IOS(十六)UI之XIB自訂Cell實現團購UI,iosxib

來源:互聯網
上載者:User

貓貓學IOS(十六)UI之XIB自訂Cell實現團購UI,iosxib

貓貓分享,必須精品

素材代碼地址:http://blog.csdn.net/u013357243/article/details/44926809
原文地址:http://blog.csdn.net/u013357243?viewmode=contents

先看

自訂Cell
 本次主要是自訂Cell的學習 實現自訂Cell主要有三種方法:按照使用的頻繁度排序:   XIB > 純程式碼 > StoryBoard
XIB的定義步驟

1> 建立HMTgCell.xib
2> 拽一個需要自訂的控制項,擺放其他子控制項
3> 建立一個類
* 類名要與XIB的名字保持一致
* 繼承自的子類要與XIB中的根節點的類型一致
4> 要連線之前,需要將XIB的根節點類名修改為剛剛建立的類名
5> 連線
6> 在XIB的屬性面板,指定可重用標示符

代碼學習
首先載入字典神馬的這些大家自己去代碼裡看吧,前面寫了太多了。
調整上側邊欄
//ps:建立iOS交流學習群:304570962 //可以加貓貓QQ:1764541256 或則znycat //讓我們一起努力學習吧。 //原文:http://blog.csdn.net/u013357243?viewmode=contents// 調整邊距,可以讓表格視圖讓開狀態列    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

同等效果的:

/** 隱藏狀態列 */- (BOOL)prefersStatusBarHidden{    return YES;}
資料來源方法 把Cell放進去
#pragma mark - 資料來源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.tgs.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1. 建立cell    HMTgCell *cell = [HMTgCell cellWithTableView:tableView];    // 2. 通過資料模型,設定Cell內容,可以讓視圖控制器不需要瞭解cell內部的實現細節    cell.tg = self.tgs[indexPath.row];    return cell;}
+ (instancetype)cellWithTableView:(UITableView *)tableView{    // 1. 可重用標示符    static NSString *ID = @"Cell";    // 2. tableView查詢可重用Cell    HMTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    // 3. 如果沒有可重用cell    if (cell == nil) {        NSLog(@"載入XIB");        // 從XIB載入自訂視圖        cell = [[[NSBundle mainBundle] loadNibNamed:@"HMTgCell" owner:nil options:nil] lastObject];    }    return cell;}- (void)setTg:(HMTg *)tg{    // setter方法中,第一句要賦值,否則要在其他方法中使用模型,將無法訪問到    _tg = tg;    self.titleLabel.text = tg.title;    self.iconView.image = [UIImage imageNamed:tg.icon];    self.priceLabel.text = tg.price;    self.buyCountLabel.text = tg.buyCount;}

注意在自己的xib中class要選好了,然後連線才能給力

模板提供的方法

#pragma mark - 模板提供的方法/**  初始化方法 使用代碼建立Cell的時候會被調用,如果使用XIB或者Storyboard,此方法不會被調用 */- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        NSLog(@"%s", __func__);    }    return self;}/** 從XIB被載入之後,會自動被調用,如果使用純程式碼,不會被執行 */- (void)awakeFromNib{    NSLog(@"%s", __func__);    self.contentView.backgroundColor = [UIColor clearColor];}/** Cell 被選中或者取消選中是都會被調用 如果是自訂Cell控制項,所有的子控制項都應該添加到contentView中 */- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];    if (selected) {        self.contentView.backgroundColor = [UIColor redColor];    } else {        self.contentView.backgroundColor = [UIColor greenColor];    }}
定義代理給頁尾按鈕實現功能(類比網路下載)

首先在點h裡面寫入協議

#import <UIKit/UIKit.h>@class HMTgFooterView;@protocol HMTgFooterViewDelegate <NSObject>@optional/** 視圖的下載按鈕被點擊 */- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView *)footerView;@end@interface HMTgFooterView : UIView// 代理如果使用強引用,就會產生循環參考,造成控制器和子視圖都無法被釋放,造成記憶體泄露@property (nonatomic, weak) id <HMTgFooterViewDelegate> delegate;+ (instancetype)footerView;/** 重新整理資料結束後,更新頁尾的視圖顯示 */- (void)endRefresh;@end

點m檔案的代碼

////  HMTgFooterView.m//  02-團購////  Created by apple on 14-8-21.//  Copyright (c) 2014年 itcast. All rights reserved.//#import "HMTgFooterView.h"@interface HMTgFooterView()@property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;@property (weak, nonatomic) IBOutlet UIView *tipsView;@end@implementation HMTgFooterView+ (instancetype)footerView{    return [[[NSBundle mainBundle] loadNibNamed:@"HMTgFooterView" owner:nil options:nil] lastObject];}- (IBAction)loadMore{    NSLog(@"載入更多");    // 1. 隱藏按鈕    self.loadMoreButton.hidden = YES;    // 2. 顯示提示視圖    self.tipsView.hidden = NO;    // 3.1 判斷代理是否實現了協議方法    if ([self.delegate respondsToSelector:@selector(tgFooterViewDidDownloadButtonClick:)]) {        [self.delegate tgFooterViewDidDownloadButtonClick:self];    }}/** 視圖控制器重新整理完成調用方法 */- (void)endRefresh{    // 4. 載入完成資料    self.loadMoreButton.hidden = NO;    self.tipsView.hidden = YES;}@end

代理的具體實現由control來實現,然後通知他

- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView *)footerView{    // 類比取網路上擷取資料載入資料    NSLog(@"努力載入資料中....");    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        // 獲得網路資料之後執行的操作        // 向數組中添加資料,類比網路載入完成之後的效果        NSDictionary *dict = @{@"title": @"哈哈", @"icon": @"ad_00", @"price": @"100.2", @"buyCount": @"250"};        HMTg *tg = [HMTg tgWithDict:dict];        NSLog(@"加資料前 %d", self.tgs.count);        [self.tgs addObject:tg];        NSLog(@"加資料後 %d", self.tgs.count);        // 重新整理資料        //    [self.tableView reloadData];        // 建立一個indexPath        NSIndexPath *path = [NSIndexPath indexPathForRow:(self.tgs.count - 1) inSection:0];        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];        // 通知頁尾視圖調整視圖顯示狀態        [footerView endRefresh];    });}
  • footView => controller 去工作,使用代理
  • controller => footView 去工作,直接調用footView的方法即可
設定頭部猜你喜歡上下的兩道線

這東西很簡單,就是一個自訂的xib 那兩道線就是兩個高1個像素點得View,————蘋果官方也是這麼搞得,所以就不要質疑啦。

    self.tableView.tableHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"HMTgHeadView" owner:nil options:nil] lastObject];

預先處理指令#if 1的特殊用法
預先處理指令 #if 0 所有代碼都不會執行 #endif

可以看做事高功能的注釋,這裡不會讓我們看到一片綠,但是也能起到注釋的作用,如果想用這代碼了 把0改成1,so easy!!

代理模式的特點

代理模式的特點:是父控制項(視圖控制器)監聽子控制項的事件,當子控制項發生某些事情時,通知父控制項工作!

ps:建立iOS交流學習群:304570962
可以加貓貓QQ:1764541256 或則znycat
讓我們一起努力學習吧。
原文:http://blog.csdn.net/u013357243?viewmode=contents

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.