ios实现QQ界面

来源:互联网 发布:ubuntu安装app 编辑:程序博客网 时间:2024/05/19 17:24

应师傅要求编写个QQ界面来指教下我的代码问题。

编写个QQ界面,有三个组,每个组有人,并显示在线不在线;

先看一下效果图

这里省了事因为我的图片只用了一张,如果要根据人的不同设置,只要在cell里面改一下就行了;

主要是实现点击上面分组展开内容,再点击收回去。

废话不多说,上菜:

我们先来构造数据:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSArray * InitArray =@[  
  2.                           @{  
  3.                               @"Name":@"朋友",  
  4.                               @"Group":@"YES",  
  5.                               @"Child":@[  
  6.                                       @{@"Name":@"张三",  
  7.                                         @"State":@"在线",},  
  8.                                       @{@"Name":@"王五",  
  9.                                         @"State":@"离开"}  
  10.                                       ]  
  11.                               },  
  12.                           @{  
  13.                               @"Name":@"家人",  
  14.                               @"Group":@"YES",  
  15.                               @"Child":@[@{@"Name":@"姐姐",  
  16.                                            @"State":@"在线"},  
  17.                                          @{@"Name":@"妹妹",  
  18.                                            @"State":@"在线"},  
  19.                                          @{@"Name":@"哥哥",  
  20.                                            @"State":@"离开"},  
  21.                                          @{@"Name":@"弟弟",  
  22.                                            @"State":@"离开"}]  
  23.                               },  
  24.                           @{  
  25.                               @"Name":@"陌生人",  
  26.                               @"Group":@"YES",  
  27.                               @"Child":@[  
  28.                                       @{@"Name":@"小王",  
  29.                                         @"State":@"在线"},  
  30.                                       @{@"Name":@"小李",  
  31.                                         @"State":@"离开"},  
  32.                                       @{@"Name":@"小红",  
  33.                                         @"State":@"离开"}]  
  34.                                 
  35.                               }  
  36.                           ];  

设置一个数组属性 来接受数据;

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @property (nonatomic,copyNSMutableArray * CellArray;  

好了,我们来利用数据构造界面吧;

界面很简单:

一个图片是自己的头像;

一个是自己的网名

一个在线状态

下面就是一个表格(tableview)

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface ViewController ()  
  2. {  
  3.     UITableView * TableV;  
  4. }  
  5.   
  6. @end  

设置一个tableview ;

往里面放:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. UIImageView *IconImageV = [[UIImageView alloc]initWithFrame:CGRectMake(15306060)];  
  2.     IconImageV.image = [UIImage imageNamed:@"icon.png"];  
  3.     [self.view addSubview:IconImageV];  
  4.     UILabel *NameLabel = [[UILabel alloc]initWithFrame:CGRectMake(1003010030)];  
  5.     NameLabel.text = @"雪松";  
  6.     NameLabel.textAlignment = 1;//设置文字的对其方式 居中   
  7.     [self.view addSubview:NameLabel];  
  8.     UILabel *StateLabel = [[UILabel alloc]initWithFrame:CGRectMake(1006010030)];  
  9.     StateLabel.text = @"在线";  
  10.     NameLabel.textAlignment = 1;  
  11.     [self.view addSubview:StateLabel];  
  12.       
  13.     TableV = [[UITableView alloc]initWithFrame:CGRectMake(15100290350)];  
  14.     TableV.delegate = self;  
  15.     TableV.dataSource = self;  
  16.     TableV.separatorStyle=0;  
  17.     TableV.backgroundColor =[UIColor yellowColor];  
  18.     [self.view addSubview:TableV];  

然后就是我们来构造table了

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.       
  4.     UITableViewCell *cell ;  
  5.     NSDictionary * dectionary = [_CellArray objectAtIndex:indexPath.row];  
  6.     if([dectionary objectForKey:@"Group"])  
  7.     {  
  8.         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"Group"];  
  9.         cell.textLabel.text = [dectionary objectForKey:@"Name"];  
  10.         cell.textLabel.textAlignment=0;  
  11.         cell.backgroundColor = [UIColor grayColor];  
  12.         cell.tag = 1;  
  13.     }  
  14.     else  
  15.     {  
  16.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Child"];  
  17.         cell.imageView.image = [UIImage imageNamed@"icon.png"];  
  18.         cell.textLabel.text = [dectionary objectForKey:@"Name"];  
  19.         cell.detailTextLabel.text = [dectionary objectForKey:@"State"];  
  20.     }  
  21.     return cell;  
  22.       
  23. }  

这里我想说一下,这个tag ,我想了很半天,如何用来记录这个分组是打开的还是折叠的呢,没有办法,最后想到了tag 用tag=1表示折叠的,=2表示是打开的;

下面最难的就是,折叠打开的方式了;

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithDictionary:[_CellArray objectAtIndex:indexPath.row]];  
  4.       
  5.     UITableViewCell *cell = [TableV cellForRowAtIndexPath:indexPath];  
  6.     NSLog(@"%@",cell.textLabel.text);  
  7.     if([dictionary objectForKey:@"Group"])  
  8.     {  
  9.         NSArray *ChildArray = [dictionary objectForKey:@"Child"];  
  10.         NSMutableArray *PathArray = [NSMutableArray array];  
  11.           
  12.         if(cell.tag==1)  
  13.         {  
  14.             cell.tag=2;  
  15.             for (int i =0 ;i<ChildArray.count;i++)  
  16.             {  
  17.                 dictionary = [ChildArray objectAtIndex:i];  
  18.                 [_CellArray insertObject:dictionary atIndex:i+indexPath.row+1];  
  19.                 NSIndexPath *path = [NSIndexPath indexPathForRow:i+indexPath.row+1 inSection:0];  
  20.                 [PathArray addObject:path];  
  21.             }  
  22.             [TableV insertRowsAtIndexPaths:PathArray withRowAnimation:UITableViewRowAnimationAutomatic];  
  23.         }  
  24.         else  
  25.         {  
  26.             cell.tag=1;  
  27.             NSMutableIndexSet * deleteSet= [NSMutableIndexSet indexSet];  
  28.             for (NSDictionary *dic  in ChildArray)  
  29.             {  
  30.                 NSInteger row= [_CellArray indexOfObject:dic];  
  31.                 NSIndexPath * Path = [NSIndexPath indexPathForRow:row inSection:0];  
  32.                 [PathArray addObject:Path];  
  33.                 [deleteSet addIndex:row];  
  34.             }  
  35.             [_CellArray removeObjectsAtIndexes:deleteSet];  
  36.             [TableV deleteRowsAtIndexPaths:PathArray withRowAnimation:UITableViewRowAnimationBottom];  
  37.         }  
  38.     }  
  39.       
  40. }  

上面是具体的方式,如果看不懂,那就去看我的另外一篇博客http://blog.csdn.net/u010123208/article/details/38176943

好了,QQ界面的源码在

http://download.csdn.net/detail/u010123208/7774851

欢迎下载

0 0
原创粉丝点击