[iOS基礎控制項 - 6.9.3] QQ好友名單Demo TableView

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

A.需求1.使用plist資料,展示類似QQ好友名單的分組、組內成員顯示縮排功能2.組名使用Header,展示箭頭表徵圖、組名、組內人數和上線人數3.點擊組名,伸展、縮回好友組 B.實現步驟1.編寫MVC結構  (1)根據plist檔案結構,編寫model,使用嵌套型  
 1 // 2 //  FriendGroup.h 3 //  FriendsList 4 // 5 //  Created by hellovoidworld on 14/12/12. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @class Friend;12 13 @interface FriendGroup : NSObject14 15 /** 好友組 */16 @property(nonatomic, strong) NSArray *friends;17 18 /** 好友組名 */19 @property(nonatomic, copy) NSString *name;20 21 /** 線上人數 */22 @property(nonatomic, assign) int online;23 24 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;25 + (instancetype) friendGroupWithDictionary:(NSDictionary *) dictionary;26 27 @end
 (2)自訂header,用於組名顯示,包括了箭頭圖示、組名、線上人數/組內人數 a.資料來源:FriendGroup模型
1 /** 載入資料  */2 - (void)setFriendGroup:(FriendGroup *)friendGroup
 b.在init方法中header控制項的位置尺寸資訊並沒有初始化,init方法僅僅用於分配記憶體,所以要使用初始化後的header位置尺寸資料,要在layoutSubviews方法中使用
 1 /** 子控制項布局方法  2     在init的時候,只分配的記憶體,沒有初始化控制項的尺寸,在此處header已經有了位置尺寸了 3 */ 4 - (void)layoutSubviews { 5     // 必須先調用父類的方法 6     [super layoutSubviews]; 7     8     // 1.背景 9     self.headerButtonView.frame = self.bounds;10    11     // 2.線上人數/組內總人數12     CGFloat countWidth = 150;13     CGFloat countHeight = self.frame.size.height;14     CGFloat countX = self.frame.size.width - 10 - countWidth;15     CGFloat countY = 0;16     self.onlineCountView.frame = CGRectMake(countX, countY, countWidth, countHeight);17 }
 c.使用tableView的緩衝池,在controller中實現相應dataSource方法
1 /** 自訂每個section的頭部 */2 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {3     FriendHeader *header = [FriendHeader friendHeaderWithTableView:self.tableView];4     header.friendGroup = self.friendGroups[section];5     return header;6 }
 (3)定義cell,用於顯示每個好友資訊,包括頭像、暱稱、介紹(簽名) 實現方法跟header差不多資料來源:Friend模型在這裡cell不用計運算元控制項的位置尺寸,直接使用 UITableViewCellStyleSubtitle類型的cell
 1 /** 自訂構造方法 */ 2 + (instancetype) cellWithTableView:(UITableView *) tableView { 3     static NSString *ID = @"friendCell"; 4     FriendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 5     6     if (nil == cell) { 7         cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 8     } 9    10     return cell;11 }12 13 /** 載入資料 */14 - (void)setFriendData:(Friend *)friendData {15     _friendData = friendData;16    17     self.imageView.image = [UIImage imageNamed:friendData.icon];18     self.textLabel.text = friendData.name;19     self.textLabel.textColor = friendData.isVip?[UIColor redColor]:[UIColor blackColor];20     self.detailTextLabel.text = friendData.intro;21 }
 初步可以顯示整個版面: 2.伸縮組內好友(1)在group模型建立一個標識,用來標識此組的好友是否顯示
1 /** 是否伸展顯示好友 */2 @property(nonatomic, assign, getter=isOpened) BOOL opened;
 (2)給header加上點擊事件,改變伸展標識
1         // 1.4點擊事件2         [headerButtonView addTarget:self action:@selector(headerClicked) forControlEvents:UIControlEventTouchUpInside];
 
 1 /** 點擊事件 */ 2 - (void) headerClicked { 3     // 1.伸展、隱藏組內好友 4     self.friendGroup.opened = !self.friendGroup.isOpened; 5     6     // 2.重新整理tableView 7     if ([self.delegate respondsToSelector:@selector(friendHeaderDidClickedHeader:)]) { 8         [self.delegate friendHeaderDidClickedHeader:self]; 9     }10 }
 (3)給header編寫代理協議
1 @protocol FriendHeaderDelegate <NSObject>2 /** header被點擊的代理方法 */3 @optional4 - (void) friendHeaderDidClickedHeader:(FriendHeader *) header;5 6 @end
 (4)viewController根據group模型的伸展標識,判斷是否返回當前組的row數
1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {2     FriendGroup *group = self.friendGroups[section];3     // 先檢查模型資料內的伸展標識4     return group.isOpened? group.friends.count : 0;5 }
 (5)使用viewController作為代理,監聽header的點擊事件,重新整理tableView資料
1 #pragma mark - FriendHeaderDelegate方法2 - (void)friendHeaderDidClickedHeader:(FriendHeader *)header {3     // 重新整理資料4     [self.tableView reloadData];5 }
  3.改變組的伸展標識—箭頭朝向(1)由於在header的代理中使用了tableView的 reloadData重新整理介面和資料,tableView會從新建立所有header,所以在reloadData之前改變header的外觀(比如箭頭朝向)是沒有意義的。     所以需要修改新建立的header
1 /**2     被加到父控制項之前 3     由於tableView重新整理資料後,所有header會被重新建立,所以要在這裡對箭頭朝向做出修改4 */5 - (void)didMoveToSuperview {6     // 改變箭頭朝向,順時針旋轉90度7     CGFloat rotation = self.friendGroup.isOpened? M_PI_2 : 0;8     self.headerButtonView.imageView.transform = CGAffineTransformMakeRotation(rotation);9 }
 (2)使用transform旋轉箭頭,因為原來的圖片大小非正方形而且填充方式是展開,需要做一些屬性修改
1         // 改變箭頭填充方式為置中2         [headerButtonView.imageView setContentMode:UIViewContentModeCenter];3         // 不需要裁剪箭頭圖片的邊界4         [headerButtonView.imageView setClipsToBounds:NO];
   

[iOS基礎控制項 - 6.9.3] QQ好友名單Demo TableView

聯繫我們

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