国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[iOS基礎控件-6.9.3]QQ好友列表DemoTableView

2019-11-14 19:45:28
字體:
來源:轉載
供稿:網友
A.需求
1.使用plist數據,展示類似QQ好友列表的分組、組內成員顯示縮進功能
2.組名使用Header,展示箭頭圖標、組名、組內人數和上線人數
3.點擊組名,伸展、縮回好友組
 
code source: https://github.com/hellovoidworld/QQFriendList
 
B.實現步驟
1.編寫MVC結構
Image(125)
 
Image(126)
 
(1)根據plist文件結構,編寫model,使用嵌套型
Image(127)
 
Image(128)
 
 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,用于組名顯示,包括了箭頭圖示、組名、在線人數/組內人數
Image(129)
 
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,用于顯示每個好友信息,包括頭像、昵稱、介紹(簽名)
Image(130)
 
實現方法跟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 }
 
初步可以顯示整個版面:
Image(131)
 
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 }
 
Image(132)
 
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];
 
Image(133)
 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 七台河市| 饶阳县| 灵台县| 游戏| 安福县| 土默特左旗| 五寨县| 秀山| 清徐县| 大安市| 邢台县| 达孜县| 都安| 淮安市| 环江| 郴州市| 乌拉特后旗| 黄龙县| 鸡泽县| 海门市| 海口市| 冀州市| 修文县| 广东省| 饶阳县| 遵化市| 万源市| 五指山市| 建宁县| 白城市| 隆昌县| 舟山市| 江永县| 苍南县| 岑溪市| 瓦房店市| 酒泉市| 南岸区| 沁水县| 郸城县| 温泉县|