1. 程式人生 > >2015年07月09日第七天筆記

2015年07月09日第七天筆記

團購思路歸納

1.自帶cell

-(UITableViewCell *) tableView:(UITableView *)TableView cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
    //定義一個靜態標識(命名和業務邏輯有關)
    static NSString *ID = @“groupPusrchase”;
    //到緩衝池去取到
    UITabelView *cell = [TableView dequeueResuableCellWithIndentifier:ID];
    //判空是則建立
    if
(cell == nil ) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //取模型資料 AMode *amode = self.arrayOfData[indexPath.row]; //往cell控制元件裡塞資料 cell.imageView.image = +++; cell.taxtLabel.text = ++; cell.detailTextLabel.text
= +++; //返回cell }

2.用xib檔案自定義cell,有xib對應的類解決方案:繼承自UITableViewCell
思路整理:反推
首先在cellForRowAtIndexPath有明顯的改動之處:不在控制器直接想cell控制元件塞資料
這是因為建立了一個自定義的cell類,於是向cell裡放資料的形式放生裡改變
改變:將從控制器中取到的模型資料,放到了cell的模型模型屬性了
具體實現:
1)就是讓定義的cell類,有個模型資料接收器,用來接收控制器(外界)傳來的資料並進行處理
2)在cell類裡定義個模型屬性,然後重寫這個屬性的setter方法(.h)

@property(nonatomic,strong)HMTgModel *tgModel;

3)在setter方法裡完成,對cell控制元件子控制元件的資料填充

-(void) setTgModel:(HMTgModel *)tgModel
{
    _tgModel = tgModel;//這句是為了和getter配套的標準寫法
    self.子控制元件1.image=[UIImage imageNamed:tgModel.icon];
    self.子控制元件2.text = tgModel.titile;
    self.子控制元件3.text = [NSString stringWithFormat:@“¥%@”, tgModel.price];
}

4)為更好的保證子控制元件的私有性,將子控制元件定義在.m檔案中 的延展類中,並且和xib檔案的控制元件保持連線

5)xib檔案通過在控制面板設定關聯cell類,和連線關聯

關於自定義代理的引入
1.引入之前 (將控制器類引入自定義的xib類中)
例子:模擬載入更多的資料
點選自定義控制元件的按鈕後,自動生成一條記錄,現在要把記錄新增到原檢視中

1)需要原檢視的陣列,滾動View在原檢視,重新整理視圖表現在原檢視,於是將原檢視引入自定義控制元件類意味著這些條件都具備了。
為了更好的解決一個問題,在自定義類裡面引入外界類,通過這個類,呼叫這個類提供的方法解決。

2)就是把控制器傳給footerView.在footerView中呼叫控制器的loadMoreData方法

-(void)loadMoreData
{
    HMTgMode *tgModel = [[HMIgMode alloc] init];
    tgModel.icon = @“”;
    tgModel.tite
    [self.tgs addObject:tgModel]; 
    [self.tableView reloadData];
    NSIndexPath *indexPath =  [NSIndxPath indexPathForRow:self.tags.count-1 inSection:0];
    [self.tableView srcollToAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

2 引入代理:減少Controller和footerView互相依賴,降低耦合性,
於是自定義的控制元件建立代理協議,定義代理屬性,當發生事件的時候通知代理
為什麼控制元件自己沒有解決事件的能力。因為和代理交織的東西太多。代理解決起來,比較方便。
自定義的控制元件,只負責更好的展示資料,和樣式
團購總結:
1)頭部:只是簡單的xib運用
2)中部:涉及到展示外界資料,引入重寫setter方法
3)尾部:涉及載入新的資料,引入監聽事件,進而引入代理

新浪微博思路歸納
1 、根據資料模型建立frame模型
之所以建立frame模型,是為了封裝計算cell內各個控制元件位置的過程。這體現了OOP的程式設計思想!
在Frame模型中,各個控制元件的Frame會根據傳遞過來的資料模型進行計算設定,所以Frame模型中必須有一個數據模型屬性,
且計算各個控制元件的frame的過程應該放在模型資料屬性的set方法中,並且在這裡計算出cell的高度

2、引入frame模型就是解決cell高度適配問題,但是完全可以寫在cell類裡類實現,但是為什麼非得要引入一個frame模型類呢
主要原因是返回行高的的tableView代理方法,先於cell類物件的建立完成呼叫,這樣,寫在cell類裡將不能計時被計算出來,
也就不能實時適配。
3.frame模型構建,基於問題,和方便以後的擴充套件,構建如下
1)在cell類裡有個frame模型屬性 行高的返回時機決定

2)在fram類有個資料模型屬性 資料大小決定

4.外界控制器的改動,由以前定義一個存放資料模型的陣列改為定義一個存放fram摸型的陣列

8.控制元件大小的計算過程 在frame模型的資料模型資料的setter方法裡
1)設定頭像的frame
必備資料:填充值
X = 填充值;Y=填充值; W = 40;H=W;
2)設定名稱
必備資料:文字真實寬度,真實高度 (動態的)
X = 頭像右邊X+填充值
Y = 頭像下邊Y+(頭像高度-文字真實高度)*0.5
W= 文字真實寬度,H= 文字真實高度

文字所佔最大範圍的計算 boundingRectWithSize

名稱最大範圍參照值:CGSize nameSize = CGSizeMake(MAXFLOAt,MAXFLOAt);

CGSize maxNameSize = [self textWithString:self.microBlog.name andFont:[UIFont systemFontOfSize:17] maxSize:nameSize];

最終會呼叫一個boundingRectWithSize方法計算 

- (CGSize)textWithString:(NSString *)text andFont:(UIFont *)font maxSize:(CGSize )maxSize{

       NSDictionary *nameDict = @{NSFontAttributeName:font};

       return   [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil].size;


}

3)設定Vip
X = 文字右邊X + 填充資料
Y = 文字Y //和文字水平對齊
W = 15
H = W;
4)設定正文
參照值 :CGSize textSize = CGSizeMake(355, MAXFLOAT);
X = 頭像X //和頭像豎直對齊
Y = 頭像下面Y + 填充資料
H = 正文真實高度,W=正文真實寬度

5.配圖
1)要判斷有無配圖
有:
X = 文字X
Y = 文字下邊Y+填充資料
W=100;
H=W;
2)計算cell高度=配圖下邊Y + 填充資料
無:
1)計算cell高度=文字下邊Y + 填充資料

9.cell重用有個問題,就是當下條記錄和取出來的cell的記錄屬性不一一匹配的時候,會導致保留取出記錄的部分資料,因為不能完全覆蓋掉。所以這個時候在賦值的時候,先清空。

細節歸納
1.當我建立一個xib類時,繼承UITableViewCell
1)在.h檔案中定義資料模型屬性的強引用
2)在.m檔案中類延展裡定義子控制元件屬性並連線
3)重寫資料模型屬性的setter方法,將資料塞入子空間中
4)注意這裡為什麼要在.h檔案中定義資料模型屬性,並在setter方法裡賦值呢?
主要是為了以後好擴充套件:比如在需要利用操作資料時,可以通過子控制元件間接獲得
5) 細節:1)設定xib檔案繼承xib類2)設定xib檔案標識 用來作cell重用
6)當在控制器裡建立cell的時候,應該從xib檔案里加載

2.當我建立一個loadView時,繼承UIView
1)當我處理載入菊花,和載入文字控制元件時,應該將它們存放在一個控制元件裡,容易同時操控隱藏和消失
2)在.h檔案中,只有代理屬性和協議。注意協議方法的命名 引數至少要返回當前View類物件
3) 把載入按鈕的點選方法放在xib類裡 為了修改相應控制元件狀態 並通知代理載入資料
4) 把菊花在面板上設定動畫 behavior:Animating 或者程式碼實現:[self.菊花 startAnimating];[self.菊花 stopAnimating]

3.當我在外界控制器設定代理時
1)把代理協議引入
2)用xib檔案建立一個物件,設定代理為當前控制器
3)這個物件賦給將tableView的尾部view tableFooterView
4) 將當前按鈕傳過去
5)延遲載入方法

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       //       3.載入更多資料
              if ([self.delegate respondsToSelector:@selector(tgFooterViewDidClick:)]) {//先判斷有沒有實現
                      [self.delegate tgFooterViewDidClick:self];
              }

4.實現協議方法
1)建立一個物件資料
2)知識儲備

//       2.重新整理資料
       [self.tableView reloadData];
//       3.顯示完整的載入的資料
       NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tgs.count - 1 inSection:0];
       [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

5.頭部圖片輪播器分析
1.定義一個xib類,一個xib檔案(scrollView,pageController,view1,view2,label 同級)
2.類裡只提供從檔案載入控制元件生成類物件的方法

+ (instancetype) groupBuyheaderView
{
    return [[[UINib nibWithNibName:@"CZGroupBuyHeaderView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
}

3.圖片載入放在- (void)awakeFromNib

4.類延展 滾動控制元件 頁面控制 定時器

新浪微博細節歸納

5.建立cell的的時候,要自定義的類中中重寫
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

6.建立cell的的時候,要將自定義的子控制元件新增到 self.contentView 裡,記住先新增再作其他操作

團購程式碼複習
1、在外部—尾部自定義xib控制元件 實現載入bundle
1)外部程式碼

在viewDidLoad
LoadBtnView *loadBtnView = [[NSBundle mainBundle] loadNibNamed:@“loadView”owner:nil options:nil].lastObject;
LoadBtnView *loadBtnView = [[NSBundle mianBundle] loadNibNamed:@“loadView”owner:nil options:nil].lastObject;
LoadBtnView *loadBtnView = [[NSBundle mainBundle] loadNibNamed:@“loadView”owner:nil options:nil].lastObject;

指定代理:自定義的按鈕在類裡監聽事件,監聽到就交給代理處理 代理的指定放在外部,代理方法的實現在外部實現

self.tableView.tableFooterView = loadBtnView;//指定自定義的View
self.tableView.tableFooterView = loadBtnView;
self.tableView.tableHeaderView = loadBtnView;
self.tableView.tableHeaderView = loadBtnView;
self.tableView.tableHeaderView = loadBtnView;

滑動到指定位置
[self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
[self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
[self.tableView sctollToRowAtIndexPath:IndexPath atScrollPosition:UITableVIewScrollPositionBottom animated:YES];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableVIewScrollPositionBottom animated:YES];
建立路徑索引
NSIndexPath *indexPath = [NSIndexPath indexPath:self.array.count-1 inSection:0];
NSIndexPath *indexPath = [NSIndexPath indexPath:self.array.count -1 inSection:0];
NSIndexPath *indexPath = [NSIndexPath indexPath:self.array.count -1 inSection:0];
NSIndexPath *indexPath = [NSIndexPath indexPath:self.array.count - 1 inSection:0];

在自定義頭部類,實現-(awakeFromNib)
-(void)awakeFromNib
{
    CGFloat imageW = self.scrollImageView.frame.size.width;
    CGFloat imageH = self.scrollImageView.frame.size.height;
    self.scrollImageView.conentSize = CGSizeMake(count * imageW,0);
      self.scrolIImageView.contentSize = CGSizeMake(count * imageW,0);
    self.sccrollImageView.contetSize = CGSizeMake(count *imageW,0);
    //設定是否分頁
    self.scrollImageView.pagingEnabled = YES;
    self.scrollimagView.pagingEnabled = YES;
    self.scrollimagView.pagingEnabled = YES//設定是否顯示水平滾動條
    self.srcollimageView.showsHorizontablScrollIndicator = NO;
    self.srcollimageView.showsHorizontalScrllIndicator = NO;
    self.scrollimageView.showsHorizontalScrollIndicator = NO;
}
//定義定時器
NSTimer *timer = [NSTimer timeWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
NSTimer *timer = [NSTimer timeWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
NSTimer *timer = [NSTimer timeWithTImeInterval:2 target:self selector:@selector(nextImage) userinfo:nil repeats:YES];
NSTimer *timer = [NSTimer timeWithTimerInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES]

//加入主執行迴圈
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.timer = timer;

終止時間
[self.timer invalidate];
[self.timer invalidate];
[self.timer invalidate];
[self.timer invalidate]

獲取頁面控制元件的當前頁

self.pageControl.currentPage;
self.pageCOntro.currentPage;
self.pageControl.currentPage;

設定滾動控制元件移動的位置
[self.scrollImageView setContentOffset:CGPointMake(x,y) animatied:YES];
[self.scrollImageViw setContentOffset:CGPointMake(x,y) animated:YES];
[self.scrollImageView setContentOffset:CGPointMake(x,y) animates:YES];

當開始滾動時
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
-(void)scrollViewWillBeginDragging:(UIscrollView *)scrollView
-(void)scrollViewWillBeginDragging:(UIScrollView *)scollView

當滾動結束時
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
-(void)scrollViewDidScroll:(UIScrollView *)scrolView;

當拖動後手指離開滑鼠時
-(void)scrollViewDidEndDecelerating:(UIScrollView)scrollView

回顧:.在控制器裡到plist取資料,就是在重寫模型屬性的getter方法的時候

-(NSArray *)模型資料屬性
{
    if(_模型資料屬性 == nil)
    {
        1、從檔案裡返回陣列
        NSArray * dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResuorse:@"A.plist" ofType:nil];
        2.檔案陣列轉換為模型陣列
        NSMutableArray *tempArray = [NSMutableArray array];
        for(NSDictionary *dict in dictArray)
        {
           AMode *amode = [AMode amodeWithDict:dict];
           [tempArray addObject:amode];
        }
         _模型資料屬性 = tempArray;
    }
      return _模型資料屬性;
}