Objective-c——UI基礎開發第九天(QQ好友名單)

來源:互聯網
上載者:User

標籤:

一、知識點:

  1、雙模型的嵌套使用

  2、Button的對齊

  3、最佳化UITableView的載入

  4、layoutSubview的使用

  5、cell的摺疊代理

二、雙模型的嵌套定義:

注意是將self.friends 尚未字典轉模型進行的操作

二、cell的重用定義方式

方法一

QQCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];

    /**

     但是這種方法,如果不是在xib中定義了identifier是不會被重用的,使用方法二進行重用

    if(cell==nil)

    {

        cell=[[NSBundle mainBundle]loadNibNamed:@"QQCell" owner:nil options:nil].lastObject;

    }

     */

 方法二

/**

     到tableview註冊一個重用的cell

     NibName:xib的檔案名稱

     bundle:傳空就是預設當前的bundle

     沒有使用xib registerClass 假設沒有和xib進行關聯

     */

    UINib *nib=[UINib nibWithNibName:@"QQCell" bundle:nil];    

    [self.tableView registerNib:nib forCellReuseIdentifier:@"QQCell"];

 

方法二的不需要再判斷cell是否為空白  就可以在cellforRowAtIndexPath 在做cell是否為nil的判斷就可以省略掉

 總結:

使用類註冊

self.tableview registerClass :forCellReuseIdentifier:

使用xib註冊

UINib *nib =[UINib nibWithNibName :@“QQCell” bundle:nil];

[self.tableview registerNib:nib forCellReuseIdentifier:@“QQCell”];

 

三、button的布局

    /**

     重點怎麼設定button 中text 和 image 都是靠左對齊,且image和text保持一定距離

     1、設定contentHorizontalAlignment(UIControlContentHorizontalAlignmentCenter/left/Right/Fill)

     2、設定text 和 image 的內邊距(imageEdgeInsets/titleEdgeInsets)

     */

     headerButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;

    //設定圖片或文本的內邊距 分別採用 imageEdgeInsets/titleEdgeInsets  UIEdgeInsetsMake

    headerButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    headerButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);

 

4.2)圖片的旋轉,問題:圖片旋轉之後可能會出現展開狀況:

 

    /**

     為了在旋轉之後保持原有的形狀(contentMode)

     UIViewContentModeScaleToFill 展開填充

     UIViewContentModeScaleAspectFit 自適應

     UIViewContentModeScaleAspectFill 自適應填充

     UIViewContentModeCenter,保持原有的尺寸

     */

     headerButton.imageView.contentMode = UIViewContentModeCenter;

    #pragma mark 發現超出父view 的邊界部分將會被切掉 修改屬性 clipsToBounds

    headerButton.imageView.clipsToBounds =NO;

 

四、tableview 的重用 (類似cell的重用)

 /**

     1、定義一個重用標識符

     2、到緩衝池中去找

     3、判斷是否為空白

     4、對headerview進行賦值並返回

     */

    static NSString *headerIdentifier [email protected]"HeaderView"; 

    HeaderView *headerView =[tableView dequeueReusableHeaderFooterViewWithIdentifier:headerIdentifier];

    if(nil==headerView)

    {

        headerView=[[HeaderView alloc]initWithReuseIdentifier:headerIdentifier];

    }

注意再HeaderView的類中 //外部調用的是哪個執行個體化方法,那麼就重寫哪個方法

所以使用:

 if(self =[super initWithReuseIdentifier:reuseIdentifier])

五、在HeaderView中,載入的子view不能顯示的原因:(LayoutSubview)

  view無法顯示的原因有:

  1、顏色與父view相同

  2、沒有添加到父view

  3、沒有設定Frame

  4、透明度

  5、被別的控制項遮蓋

  6、hidden=yes

  7、檢查父view的上述情況

注意:使用subview就相當於對控制項進行一個強引用

//layer:布局 當view 的frame發生改變的時候就會調用

/**

 如果在執行個體化的時候沒有取到當前的frame

 或者噹噹前的frame發生變化

 或者當錢frame的bounds全部為0時

 這個方法對內部的子view(frame),對控制項進行設定

 */

-(void)layoutSubviews

{#warning 一定要調用父類的方法 因為這個是針對frame發生改變時調用的,所以不應該在這裡做添加 只做frame 的設定,其它東西別動

    [super layoutSubviews];

 

六、cell的摺疊代理添加協議

1、設定代理屬性

@class HeaderView;

@protocol HeaderViewDelegate<NSObject>

-(void)headerView:(HeaderView *) headerView didClickButton :(UIButton *)button;

 @property (nonatomic,weak) id<HeaderViewDelegate> delegate;

 

2、通知代理

-(void)didClickButton:(UIButton *)button

{if([self.delegate responsToSelector:@selector(headerView:didClickButton:)])

{[self.delegate headerView:self didClickButton:button];}}

 

3、在viewcontroller中 遵守協議實現代理方法

headerView.delegate=self;

 -(void)headerView:(HeaderView*)headerView didClickButton:(UIButton*)button{

}

 提示:

numberofrowsInSection返回0時 將不執行cellForRowAtIndexPath

 

 七、tip如何在代理中取出模型對應的section 並對model中的變數進行修改(tag)

在tableview的 viewForHeaderInSection重用方法中:

//擷取哪一組

 headerView.tag=section;

 在協議方法中擷取section

 //1、 擷取section的數值

NSInteger section = headerView.tag;

 

 NSInteger section = headerView.tag;

    

    //2、擷取groupModel

    GroupsModel *groupModel = self.dataArray[section];

    

    //3、對groupModel中的isExplain變數進行修改

    groupModel.explain =!groupModel.isExplain;

    

    //4、重新整理表格,為什麼explain明明設定為yes了還是沒用,因為tableview已經調用過numberofrowInsection方法,要想重新調用,需要使用reload重新整理

    //[_tableview reloadData];

    NSIndexSet *indexSet =[NSIndexSet indexSetWithIndex:section];

    [_tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

    

#pragma mark 這裡有一個問題,為什麼三角形映像會點擊一次加一次 就會出現類似瞬間變回原樣的狀態? 與reload的重新整理有關,重新整理將會讓tableview的所有代碼又重新執行一遍

 

 

 

 

 

 

 

 

    

 

Objective-c——UI基礎開發第九天(QQ好友名單)

聯繫我們

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