iOS UITableView制作类似QQ好友列表视图

           

1 #import <UIKit/UIKit.h>
2
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4
5 @property (strong, nonatomic) UIWindow *window;
6
7
8 @end
 1 #import "AppDelegate.h"
 2 #import "RootViewController.h"
 3 @interface AppDelegate ()
 4
 5 @end
 6
 7 @implementation AppDelegate
 8
 9
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12     // Override point for customization after application launch.
13     self.window.backgroundColor = [UIColor whiteColor];
14
15     UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
16     self.window.rootViewController = navi;
17
18     [self.window makeKeyAndVisible];
19     return YES;
20 }
21
22 @end
1 #import <UIKit/UIKit.h>
2
3 @interface RootViewController : UIViewController
4
5 @end
  1 #import "RootViewController.h"
  2
  3 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
  4 {
  5     UITableView *_tableView;
  6     NSMutableDictionary *dataDic;
  7 }
  8 @end
  9
 10 @implementation RootViewController
 11
 12 - (void)viewDidLoad {
 13     [super viewDidLoad];
 14     // 初始化_tableView
 15     [self initializeTableView];
 16     // 加载数据
 17     [self loadData];
 18 }
 19 /**
 20  *  初始化_tableView
 21  */
 22 - (void)initializeTableView
 23 {
 24     _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
 25     _tableView.dataSource = self;
 26     _tableView.delegate = self;
 27     [self.view addSubview:_tableView];
 28 }
 29 /**
 30  *  加载数据
 31  */
 32 - (void)loadData
 33 {
 34     // 数组的第一个对象为标志位(是否展开)
 35     NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"0",@"apple",@"alex",@"alert",@"awake", nil];
 36     NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"0",@"blance",@"bank",@"baby",@"bet",@"balance", nil];
 37     NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"0",@"cake",@"cat",@"caught",@"cell",@"clabe", @"cry",@"cave",nil];
 38     NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"0",@"dog",@"data",@"date",@"drive",@"down", @"deliver",@"dire",@"drawn",@"dety",@"depature",@"dom",nil];
 39     NSMutableArray *arr5 = [NSMutableArray arrayWithObjects:@"0",@"elphance",@"eleven",@"every",nil];
 40     // 把对应的数据存入字典
 41     dataDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"A",arr2,@"B",arr3,@"C",arr4,@"D",arr5,@"E", nil] ;
 42 }
 43
 44 #pragma mark - UITableViewDataSource And UITableViewDelegate -
 45 // 返回的表头个数
 46 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 47 {
 48     return [[dataDic allKeys] count];
 49 }
 50 // 每个表头返回对应的行数
 51 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 52 {
 53     // 因为字典是无序的,通过比较来进行排序
 54     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
 55     NSString *key = keys[section];
 56     NSMutableArray *array = dataDic[key];
 57     NSString *IsExpand = array[0];
 58     // 如果为“1”则返回相应的行数,否则返回0
 59     if ([IsExpand isEqualToString:@"1"]) {
 60         // 因为数组的第一位为标志位所以减1
 61         return ([array count] - 1);
 62     }
 63     return 0;
 64 }
 65
 66 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 67 {
 68     return 45;
 69 }
 70
 71 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
 72 {
 73     return 1;
 74 }
 75
 76 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 77 {
 78     return 60;
 79 }
 80
 81 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
 82 {
 83     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
 84     view.backgroundColor = [UIColor colorWithRed:206 green:206 blue:206 alpha:1];
 85     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width - 20, 45)];
 86     label.font = [UIFont systemFontOfSize:30];
 87     label.backgroundColor = [UIColor clearColor];
 88     label.textColor = [UIColor blackColor];
 89     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
 90     label.text = keys[section];
 91     [view addSubview:label];
 92     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 93     button.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45);
 94     [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
 95     button.tag = 1000 + section;
 96     [view addSubview:button];
 97     return view;
 98 }
 99
100 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
101 {
102     static NSString *identifier = @"cell";
103     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
104     if (cell == nil) {
105         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
106     }
107     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
108     NSString *key = keys[indexPath.section];
109     NSMutableArray *array = dataDic[key];
110     // 由于数组的第一位是标志位,且不用显示,所以加1
111     NSString *textStr = array[indexPath.row + 1];
112     cell.textLabel.text = textStr;
113     return cell;
114 }
115
116 #pragma mark -Target Action-
117 /**
118  *  点击表头,如果没有展开,则展开;如果已经展开,则关闭
119  */
120 - (void)buttonAction:(UIButton *)sender
121 {
122     long section = sender.tag - 1000;
123     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
124     NSString *key = keys[section];
125     NSMutableArray *array = dataDic[key];
126     NSString *IsExpand = array[0];
127     // 如果IsExpand等于“0”(关闭状态),则展开,且设置其值为“1”;相反,如果IsExpand等于“1”(展开状态),则关闭,且设置其值为“0”
128     if ([IsExpand isEqualToString:@"0"]) {
129         array[0] = @"1";
130     }else{
131         array[0] = @"0";
132     }
133     [_tableView reloadData];
134 }
135 @end
时间: 2024-07-30 10:19:01

iOS UITableView制作类似QQ好友列表视图的相关文章

基于Qt的类似QQ好友列表抽屉效果的实现

前段时间在忙毕业设计,所以一直没有更新博客.今天答辩完以后,将对我的毕业设计进行模块展示,供Qt初学者进行参考. 毕业设计题目:Linux系统下基于Qt的局域网即时通信系统设计与实现 其中我有一个类似于QQ的好友列表,然后对好友可以进行分组管理,毕设中具体效果图如下: 网上查寻到的设计思路: 1.采用QToolBox的方式,虽然看起来有点样子,但是并不是我们所熟悉的好友列表,比如:http://blog.csdn.net/qianguozheng/article/details/6719074

UI基础--UITableView实现仿QQ好友列表页面

需求:类似于QQ好友列表页面的显示,有好友分组,有好友数量,在线人数,vip会员.展开分组时显示分组好友,合并分组时不显示:具体效果图如下: 分析: 1.展开分组时显示分组好友,该功能可以使用显示UITableViewCell的数据即可: 2.分组头可以考虑使用一个headerView来实现: 示例文件结构: 具体实现步骤: 1.自定义数据模型类,由于plist文件中包含了2个字典,所以需要写2个数据模型: 2.自定义cell,属性包括数据模型以及生成可重用cell的方法,由于系统自带的子控件即

【iOS开发-67】QQ好友列表案例:UITableViewHeaderFooterView类、layoutSubviews与didMoveToSuperView方法等

(1)效果 (2)源代码于素材下载 http://pan.baidu.com/s/1jfdr4 (3)总结 --有标记状态的属性,应该在对应的模型类中,并且把getter方法重命名为isXXX: --UITableViewHeaderFooterView不可以用xib,只能用代码创建. --layoutSubviews方法,是当改变父控件高度的时候,自动调用这个方法,所以一般在这里面设置子控件的frame.因为子控件随着父控件而改变.这里要注意的时,初始化方法中父控件没有frame,所以子控件设

[iOS基础控件 - 6.9.3] QQ好友列表Demo TableView

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

【iOS基础控件 - 13】【Demo 】QQ好友列表TableView

A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组 code source: B.实现步骤 1.编写MVC结构 (1)根据plist文件结构,编写model,使用嵌套型 1 // 2 // FriendGroup.h 3 // FriendsList 4 // 5 // Created by hellovoidworld on 14/12/12. 6 // Copyr

ExpandableListView仿QQ好友列表

本例中,对ExpandableListView中的数据进行了封装,分为两个JavaBean,一个为Group类表示组信息,一个Child类表示该组下子列表信息: Group: public class Group { private String groupName;//分组名 private List<Child> childList;//该分组写子列表 public String getGroupName() { return groupName; } public void setGro

iOS TableView实现QQ好友列表(三)

上节我们讲到如何展示好友信息 iOS TableView实现QQ好友列表(二) http://blog.csdn.net/lwjok2007/article/details/46549111 接下来我们将分组点击的时候折叠起来. 首先新建一个可变字典用来存储当前列表是否展示 NSMutableArray *selectedArr;//控制列表是否被打开 selectedArr=[[NSMutableArray alloc]init]; 根据前两节所讲,我们讲分组名称放在section的heade

iOS-QQ好友列表 iOS 页面间几种传值方式(属性,代理,block,单例,通知)

主要是 点击按钮实现下拉 刷新数据 页面间传值 // // HMFriendsModel.h // QQ好友列表 // // Created by YaguangZhu on 15/9/1. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <Foundation/Foundation.h> @interface HMFriendsModel : NSObject @property(nonatomic,cop

tableView练习 -- QQ好友列表

LWTViewController.h // // LWTViewController.h // tableView练习 -- QQ好友列表 // // Created by apple on 14-6-1. // Copyright (c) 2014年 lwt. All rights reserved. // #import <UIKit/UIKit.h> @interface LWTViewController : UITableViewController @end LWTViewCon