- (void)viewDidLoad {[super viewDidLoad];//malloc开辟内存空间_flag = malloc(sizeof(BOOL)*10);//memset设置某块内存区域的值memset(_flag, 0, sizeof(BOOL)*10);NSMutableArray *friend = [[NSMutableArray alloc] initWithObjects:@"小明",@"大壮",@"楚中天", nil];NSMutableArray *classmate = [NSMutableArray arrayWithObjects:@"韩梅梅",@"李雷",@"林涛",@"丽丽", nil];NSMutableArray *family = [NSMutableArray arrayWithObjects:@"爸爸",@"妈妈",@"哥哥",@"姐姐", nil];_friendDic = [[NSDictionary alloc] initWithObjectsAndKeys:friend,@"朋友",classmate,@"同学",family,@"家人", nil];[friend release];_table.delegate = self;_table.dataSource = self;//_table.sectionHeaderHeight = 40;//_table.sectionFooterHeight = 30;
}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{if (_flag[section]) {return 30;}else{return 0;}
}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{return 40;
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return _friendDic.count;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//如果这个区是关闭状态那么返回0行。if (!_flag[section]) {return 0;}//allValues得到一个数组,数组中的内容是字典中所有的值,NSArray *allFriendsArray = [_friendDic allValues];//先从大数组中找到这个区对应的小数组NSArray *thisSectionArray = [allFriendsArray objectAtIndex:section];//返回这个区的数组的对象个数return thisSectionArray.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *identifier = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];if (!cell) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];}//allValues得到一个数组,数组中的内容是字典中所有的值,NSArray *allFriendsArray = [_friendDic allValues];//先从大数组中找到这个区对应的小数组NSArray *thisSectionArray = [allFriendsArray objectAtIndex:indexPath.section];//再根据行号从小数组中找到这一行对应的人名cell.textLabel.text = [thisSectionArray objectAtIndex:indexPath.row];return cell;
}//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
//        //allKeys获得一个数组,数组中得内容是字典的所有键
//    return [[_friendDic allKeys] objectAtIndex:section];
//}//设置区头的视图(自定义区头)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];//区头的view不需要设置frame,因为区头的宽度取决于tableView的宽度,区头高度通过其他方式设置//allKeys获得一个数组,数组中得内容是字典的所有键NSString *sectionName = [[_friendDic allKeys] objectAtIndex:section];[button setTitle:sectionName forState:UIControlStateNormal];[button addTarget:self action:@selector(headerButtonClick:) forControlEvents:UIControlEventTouchUpInside];button.tag = section;return button;
}- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{if (_flag[section]) {UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];[button setTitle:@"添加新好友" forState:UIControlStateNormal];[button addTarget:self action:@selector(footerButtonClick:) forControlEvents:UIControlEventTouchUpInside];button.tag = section;return button;}else{return nil;}
}- (void)footerButtonClick:(UIButton *)sender{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加新好友" message:@"请输入好友姓名" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];//alertViewStyle 警告框的样式,可以设置为带输入框的样式。alert.alertViewStyle = UIAlertViewStylePlainTextInput;alert.tag = sender.tag;UITextField *field = [alert textFieldAtIndex:0];field.placeholder = @"请输入姓名";[alert show];[alert release];
}- (void)headerButtonClick:(UIButton *)sender{//每次点击某个区的button时,对这个区的打开关闭状态取反,然后刷新table,就可以实现打开关闭_flag[sender.tag] = !_flag[sender.tag];//动画重新加载某些区[_table reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{if (buttonIndex == 1) {//先找到这个区对应的数组NSMutableArray *sectionArray = [[_friendDic allValues] objectAtIndex:alertView.tag];UITextField *field = [alertView textFieldAtIndex:0];//把输入的名字添加到数组当中[sectionArray addObject:field.text];[_table reloadData];}
}

效果图如下:

QQ好友分组添加好友(UITableView实现)相关推荐

  1. UITableView的折叠收缩和QQ好友分组效果

    可折叠展开的tableView,QQ好友分组列表 demo下载地址https://github.com/zhengwenming/ExpandTableView   原理分析:这个可以折叠的table ...

  2. IOS 实现QQ好友分组展开关闭功能

    贴出核心代码  主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...

  3. Qt可拖拽排序表格(类似QQ好友分组排序)

    1,简介 为了最佳体验,一个拖拽行排序的功能研究了几个小时.效果参考的QQ好友分组的排序. 网上查了下好像没有人发布QT版类似的代码,于是自己动手 QQ好友分组排序效果: 2,效果 这是最终效果图,有 ...

  4. QQ好友分组模拟小程序

    QQ好友分组:一个好友组里有多个好友,一个好友只能选择一个组,这样好友组和好友之间就是一个一对多的关系.在此程序中封装一个好友类即Buddy类,一个组类即Group类.在Buddy类有有关好友的最基本 ...

  5. expandableListview的使用,模仿qq好友分组点击收缩扩展

    我主要讲述的是用listview实现.模仿qq好友分组点击收缩.扩展功能 这个是对listview的拓展,用法比较相似,还是需要一个适配器 MainActivitypublic class MainA ...

  6. js实现qq好友分组

    qq好友分组 <style>ul,h2 {padding: 0;margin: 0;background-color: wheat;}li {list-style: none;}#list ...

  7. 模仿QQ好友分组风格

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title> ...

  8. Android之实现QQ好友分组(ExpandableListView)

    在项目开发中,也许我们遇到过ListView中嵌套ListView,但谷歌建议我们最好别这样做,因此他们写好了一个ExpandableListView类,他继承ListView,可以实现ListVie ...

  9. Qt可拖拽排序表格(解决滚动条不兼容问题,类似QQ好友分组排序)

    原版链接 https://blog.csdn.net/dpsying/article/details/77206127 Qt可拖拽排序表格(类似QQ好友分组排序) 感谢博主无私分享 修改后,解决了滚动 ...

最新文章

  1. OpenCV环境下Laplace(拉普拉斯)和Roberts基本边缘检测算子的实现代码
  2. Mac下Jekyll安装
  3. 用JAVASCRIPT实现静态对象、静态方法和静态属性
  4. ClickHouse【环境搭建 01】Linux环境单机版在线安装 Code:210.DB::NetException + Init script is already running 问题处理
  5. oracle 创建新库时报错:enterprise manager 配置失败
  6. 一文看懂哈夫曼树与哈夫曼编码
  7. 为什么jdk中把String类设计成final
  8. python做神经网络有什么框架_神经网络与深度学习——基于TensorFlow框架和Python技术实现...
  9. 【bzoj1047】[HAOI2007]理想的正方形 二维RMQ
  10. Js中对id和class属性进行模糊查询
  11. @程序员,React 使用如何避坑?
  12. 敏感词库php数组,PHP 实现敏感词 / 停止词 过滤(附敏感词库),敏感类词语大全...
  13. 信息系统综合知识六 标准化与知识产权
  14. 高等工程数学(张韵华,汪琥庭,宋立功)—— 第一篇:线性代数
  15. 华为的人力资源体系的变革
  16. 变点理论CUSUM在择时交易中的应用
  17. Android开发实用小工具二——长度转换工具
  18. MySQL系列教程(四)
  19. JMF环境配置(Eclipse)
  20. 历时2个月终跳槽成功,面试经验全在这儿了!

热门文章

  1. Spring高手之路2——深入理解注解驱动配置与XML配置的融合与区别
  2. 10G网络变压器厂家告诉你10G以太网主要有哪些特点
  3. 树莓集团旗下产业园正式核名为“数媒大厦”!
  4. pymongo获取一列数据
  5. 什么叫4K对齐、如何进行硬盘4K对齐?
  6. 做加推的超级IP名片店铺原来这么赚钱!
  7. jmeter + java jdk 的下载+安装+环境配置+如何创建桌面快捷方式羽毛图标
  8. idea字体颜色修改
  9. K210、Openmv与串行总线舵机通信(基于micropython)舵机驱动板和舵机控制板代码
  10. windows系统复制大文件提示对于目标文件系统过大的解决方案