IOS总结_可收缩分组表格(仿QQ联系人界面)

#import "yxpGroupTBVC.h"

#define  DIC_EXPANDED @"expanded" //是否是展开 0收缩 1展开

#define  DIC_ARARRY @"array"

#define  DIC_TITILESTRING @"title"

#define  CELL_HEIGHT 40.0f

@interfaceyxpGroupTBVC ()<UITableViewDataSource,UITableViewDelegate>

{

UITableView *_tableVIew;

NSMutableArray *_DataArray;

}

@end

@implementation yxpGroupTBVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil

{

self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if (self) {

/*

主要思路:

1.tableView:tableView viewForHeaderInSection:section 添加一个按钮

2.点击按钮后,判断指定section的数据是否展开

3.在返回numberOfRowsInSection数量时,如果发现是收缩的,则返回0,展开时,才给真实数据的行号

这样就可以达到显示/隐含数据的效果

*/

}

returnself;

}

//初始化数据

- (void)initDataSource

{

//创建一个数组

_DataArray=[[NSMutableArrayalloc]
init];

for (int i=0;i<=5 ; i++)
{

NSMutableArray *array=[[NSMutableArrayalloc]
init];

for (int j=0; j<=5;j++)
{

NSString *string=[NSStringstringWithFormat:@"%i组-%i行",i,j];

[arrayaddObject:string];

}

NSString *string=[NSStringstringWithFormat:@"第%i分组",i];

//创建一个字典 包含数组,分组名,是否展开的标示

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]
initWithObjectsAndKeys:array,DIC_ARARRY,string,DIC_TITILESTRING,[NSNumbernumberWithInt:0],DIC_EXPANDED,nil];

//将字典加入数组

[_DataArrayaddObject:dic];

}

}

//初始化表

- (void)initTableView

{

_tableVIew=[[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

_tableVIew.dataSource=self;

_tableVIew.delegate=self;

[self.viewaddSubview:_tableVIew];

}

- (void)viewDidLoad

{

[superviewDidLoad];

[selfinitDataSource];

[selfinitTableView];

}

#pragma mark -- UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return_DataArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];

NSArray *array=[dic
objectForKey:DIC_ARARRY];

//判断是收缩还是展开

if ([[dicobjectForKey:DIC_EXPANDED]intValue])
{

return array.count;

}else

{

return
0;

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath

{

static
NSString *acell=@"cell";

UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:acell];

if (!cell) {

cell=[[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:acell];

}

NSArray *array=[[_DataArrayobjectAtIndex:indexPath.section] objectForKey:DIC_ARARRY];

cell.textLabel.text=[arrayobjectAtIndex:indexPath.row];

return cell;

}

//设置分组头的视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

UIView *hView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,
320, CELL_HEIGHT)];

hView.backgroundColor=[UIColorwhiteColor];

UIButton* eButton = [[UIButtonalloc]
init];

//按钮填充整个视图

eButton.frame = hView.frame;

[eButtonaddTarget:selfaction:@selector(expandButtonClicked:)

forControlEvents:UIControlEventTouchUpInside];

//把节号保存到按钮tag,以便传递到expandButtonClicked方法

eButton.tag = section;

//设置图标

//根据是否展开,切换按钮显示图片

if ([self
isExpanded:section])

[eButton
setImage: [ UIImageimageNamed:
@"mark_up" ]forState:UIControlStateNormal];

else

[eButton
setImage: [ UIImageimageNamed:
@"mark_down" ]forState:UIControlStateNormal];

//设置分组标题

[eButton setTitle:[[_DataArrayobjectAtIndex:section] 
objectForKey:DIC_TITILESTRING]forState:UIControlStateNormal];

[eButton setTitleColor:[UIColorblackColor]
forState:UIControlStateNormal];

//设置button的图片和标题的相对位置

//4个参数是到上边界,左边界,下边界,右边界的距离

eButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;

[eButton setTitleEdgeInsets:UIEdgeInsetsMake(5,5,
0,0)];

[eButton setImageEdgeInsets:UIEdgeInsetsMake(5,300,
0,0)];

//上显示线

UILabel *label1=[[UILabelalloc]
initWithFrame:CGRectMake(0, -1, hView.frame.size.width,1)];

label1.backgroundColor=[UIColorskyBlueColor];

[hViewaddSubview:label1];

//下显示线

UILabel *label=[[UILabelalloc]
initWithFrame:CGRectMake(0, hView.frame.size.height-1,
hView.frame.size.width,1)];

label.backgroundColor=[UIColorskyBlueColor];

[hViewaddSubview:label];

[hViewaddSubview: eButton];

return hView;

}

//单元行内容递进

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath
*)indexPath

{

return
2;

}

//控制表头分组表头高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

returnCELL_HEIGHT;

}

#pragma mark -- 内部调用

//对指定的节进行“展开/折叠”操作,若原来是折叠的则展开,若原来是展开的则折叠

-(void)collapseOrExpand:(int)section{

NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];

int expanded=[[dic
objectForKey:DIC_EXPANDED]
intValue];

if (expanded) {

[dic setValue:[NSNumbernumberWithInt:0]forKey:DIC_EXPANDED];

}else

{

[dic setValue:[NSNumbernumberWithInt:1]forKey:DIC_EXPANDED];

}

}

//返回指定节是否是展开的

-(int)isExpanded:(int)section{

NSDictionary *dic=[_DataArrayobjectAtIndex:section];

int expanded=[[dic
objectForKey:DIC_EXPANDED]
intValue];

return expanded;

}

//按钮被点击时触发

-(void)expandButtonClicked:(id)sender{

UIButton* btn= (UIButton*)sender;

int section= btn.tag;//取得tag知道点击对应哪个块

[selfcollapseOrExpand:section];

//刷新tableview

[_tableVIewreloadData];

}

@end

IOS总结_可收缩分组表格(仿QQ联系人界面)

时间: 2024-08-11 01:37:09

IOS总结_可收缩分组表格(仿QQ联系人界面)的相关文章

动手分析安卓仿QQ联系人列表TreeView控件

因项目需要需要用到仿QQ联系人列表的控件样式,于是网上找到一个轮子(https://github.com/TealerProg/TreeView),工作完成现在简单分析一下这个源码.   一. 需要用到的知识如下: ①安卓事件分发机制:(http://blog.csdn.net/lvxiangan/article/details/9309927  或 http://gundumw100.iteye.com/blog/1052270) ②安卓View绘制:http://blog.csdn.net/

亲身体验用Java写的仿qq聊天界面

Java开发工具有许多种,新手用记事本写Java程序,有些人用NetBean,jbuilder,高手用eclipse,下面介绍用eclipse开发qq聊天界面. 代码如下: package Myjava_QQ; import java.awt.*; import javax.swing.*; import Myjava_QQ.truess; import java.awt.event.*; import java.applet.*; import java.io.BufferedReader;

IOS UI学习 UITableView Demo 实现类似QQ联系人收起和展开效果

UItableView 日常学习总结 实现类似 QQ联系人收起和展开的效果 思路 就是 自定义Header 在它上面添加一个Button 或者一个点击手势 ,我是添加了一个手势 每一个分区设置一个状态为表示为收起和展开  (bool 型 即可) 当判断为收起时将分区 section的row数量设置为0,即不显示任何内容 当判断为展开时将分区 section的row数量设置为要显示的内容的数目 然后重载分区即可 重载分区方法 1 //重载分区 2 [_tableV reloadSections:[

tabview分组。仿qq列表

代码如下 #import "ViewController.h" #import "JRProvince.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> /** 省份数据数组*/ @property(nonatomic,strong) NSMutableArray * province; /** 各个地市数据*/ @property(nonatomic,s

iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)

一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 支持pod导入pod 'LFKit/LFBadge' //一般view上加角标 _badge1 = [[LFBadge alloc] init]; [_badge1 addToTabBarItem:_view1]; //BarButtonItem上加角标 _badge2 = [[LFBadge alloc] init]; [_badge2 addToBarButtonItem:self

Android—简单的仿QQ聊天界面

最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,): 其中聊天背景可以是一个LinearLayout或者RelativeLayout里面存放的是ListView(将ListView的分割线设置成透明:android:divider="#0000"否则聊天界面会显示出分割线,,,想想都屌,,,) 于是,我要上主界面的xml布局文件了: <?xml version="1.0" encoding="utf-8"?&g

Android 仿QQ消息界面

values 下面 dimens.xml <resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</di

仿QQ聊天界面&lt;一&gt;

先上图: 首先需要两个模型: 1->数据模型 2->位置模型 数据模型代码如下: Message.h文件里: #import <Foundation/Foundation.h> typedef enum { MessagesTypeMe = 0 , //自己发的 MessagesTypeOther //别人发的 }MessagesType; @interface Messages : NSObject /** * 聊天内容 */ @property (nonatomic ,copy

仿qq联系人 学习笔记---ExpandableListActivity的使用

[转]原地址  http://blog.163.com/[email protected]/blog/static/237809502011102010100331/ 效果显示图: 1.布局文件 main.xml(ExpandableListActivity布局文件) 注意事项: ExpandableListActivity的布局文件中必须包含一个ExpandableListView,并且id必须为="@id/android:list".还可以包含一个id为empty的TextView