Table View的使用(Grouped样式表格)

简介:上篇做了Table View的一些介绍 ,还做了一个TableView 的Plain样式的例子,这篇我们学习Grouped样式表的例子,还有用到前面读取Plist的知识,把Plist文件中的数据读取出来,放到Table view里展示出来。这里把全国30多个省份的城市,都列出来了,plist文件里还有城市的行政区,不过这里只列出省份和城市就ok了。效果图如下:

 

 

那么开始吧。

1、新建项目

新的一个名称为TableViewGrouped的Single View Application项目,打开项目的xib文件,拖拽TableView控件到xib文件中,摆正位置。

2、给新建的TableView找到他的归属

选中新添的TableView ,Connection Inspector,找到delegate和datasource,从它们右边的圆圈拉线到Files Owner图标上,参考上篇的第3步:

3、设置Table View的属性为Grouped样式


4、导入plist文件

从其他文件夹导入Provineces.plist文件,这个文件我会传到源代码里,大家都能方便使用了,包括全国30个省份和城市,还有城市的区也有。

 

5、添加.h .m的实现代码。

.h文件添加一个property

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. @property (strong, nonatomic) NSArray *provinces;  
  5. @end  


第一步从Plist读取出数据,第二步给Table添加数据。

在viewDidLoad读取Plist,plist是个array类型的,所以使用Array读取。

.m文件的实现。

  1. @implementation ViewController  
  2. @synthesize provinces;  
  3.   
  4. - (void)viewDidLoad  
  5. {  
  6.     [super viewDidLoad];  
  7.     // Do any additional setup after loading the view, typically from a nib.  
  8.     NSBundle *bundle = [NSBundle mainBundle];  
  9.     NSString *plistPath = [bundle pathForResource:@"Provineces" ofType:@"plist"];  
  10.     NSMutableArray *array=[[NSMutableArray  alloc] initWithContentsOfFile:plistPath];  
  11.     self.provinces = array;  
  12.   
  13. }  


实现TableView表格部分,下面这些方法看方法名就能大概明白意思。

  •  这个方法用来告诉表格有几个分组
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  2.     return [provinces count];  
  3. }  
  • 这个方法告诉表格第section个分段有多少行
  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  2.         NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"];  
  3.     return [cities count];  
  4. }  
  •    这个方法用来告诉某个分组的某一行是什么数据,返回一个UITableViewCell
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.     NSUInteger section = [indexPath section];   
  3.     NSUInteger row = [indexPath row];   
  4.       
  5.       
  6.     NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"] ;  
  7.       
  8.       
  9.     static NSString *GroupedTableIdentifier = @"cell";   
  10.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   
  11.                              GroupedTableIdentifier];   
  12.     if (cell == nil) {   
  13.         cell = [[UITableViewCell alloc]   
  14.                 initWithStyle:UITableViewCellStyleDefault   
  15.                 reuseIdentifier:GroupedTableIdentifier];   
  16.     }   
  17.       
  18.     //给Label附上城市名称  key 为:C_Name  
  19.     cell.textLabel.text = [[cities objectAtIndex:row] objectForKey:@"C_Name"];   
  20.     return cell;   
  21. }  
  • 这个方法用来告诉表格第section分组的名称 
  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {  
  2.     NSString *provincName = [[provinces objectAtIndex:section] objectForKey:@"p_Name"];  
  3.     return provincName;   
  4. }  

 

  • 重点介绍下这个方法:
  1. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {  
  2.     //返回省份的数组  
  3.     NSMutableArray *array = [NSMutableArray arrayWithCapacity:35];  
  4.     for (NSDictionary *dict in provinces) {  
  5.         [array addObject:[dict objectForKey:@"p_Name"]];  
  6.     }  
  7.     return array;  
  8. }  
返回所有省份名称的数组 ,通过点击右边的省份名称能快速定位到这个省份的城市,也就是快速定位到这个section。

OK,运行。效果如下:

试试改成plain样式的分段TableView看看:

以上例子的全部

例子的代码:http://download.csdn.net/detail/totogo2010/4361876

https://github.com/schelling/YcDemo/tree/master/TableViewGrouped

转载于:https://my.oschina.net/jackyyang/blog/67125

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Jupyter中,groupedgroupby都是用于对数据进行分组操作的函数。其中groupby是pandas库中的函数,而grouped是DataFrame对象的方法。 下面是groupby的使用方法: 假设我们有一个DataFrame对象df,其中包含学生的姓名、班级、性别和成绩等信息。我们可以使用groupby函数按照班级和性别对学生进行分组,并计算每个组的总分、平均分和标准差: ```python import pandas as pd import numpy as np df = pd.DataFrame({'姓名': ['张三', '李四', '王五', '赵六', '钱七', '孙八'], '班级': ['一班', '一班', '二班', '二班', '三班', '三班'], '性别': ['男', '男', '女', '女', '男', '女'], '成绩': [80, 90, 85, 95, 70, 75]}) grouped = df.groupby(['班级', '性别']).agg([np.sum, np.mean, np.std]) print(grouped) ``` 输出结果为: ``` 成绩 sum mean std 班级 性别 一班 女 0.0 NaN NaN 男 170.0 85.0 7.071068 三班 女 75.0 75.0 NaN 男 70.0 70.0 NaN 二班 女 95.0 95.0 NaN 男 90.0 90.0 7.071068 ``` 上面的代码中,我们首先创建了一个DataFrame对象df,然后使用groupby函数按照班级和性别对学生进行分组。接着,我们使用agg函数计算每个组的总分、平均分和标准差,并将结果保存在grouped对象中。最后,我们打印出grouped对象的内容。 下面是grouped使用方法: 假设我们有一个DataFrame对象df,其中包含学生的姓名、班级、性别和成绩等信息。我们可以使用groupby方法按照性别对学生进行分组,并计算每个组的基本统计量: ```python import pandas as pd df = pd.DataFrame({'姓名': ['张三', '李四', '王五', '赵六', '钱七', '孙八'], '班级': ['一班', '一班', '二班', '二班', '三班', '三班'], '性别': ['男', '男', '女', '女', '男', '女'], '成绩': [80, 90, 85, 95, 70, 75]}) grouped = df.groupby('性别').describe().unstack() print(grouped) ``` 输出结果为: ``` 成绩 count mean std min 25% 50% 75% max 性别 女 3.0 85.0 10.606602 75.0 80.0 85.0 90.0 95.0 男 3.0 80.0 10.408330 70.0 75.0 80.0 85.0 90.0 ``` 上面的代码中,我们首先创建了一个DataFrame对象df,然后使用groupby方法按照性别对学生进行分组。接着,我们使用describe方法计算每个组的基本统计量,并将结果保存在grouped对象中。最后,我们打印出grouped对象的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值