zoukankan      html  css  js  c++  java
  • IOS学习:UITableView使用详解3 分组表的简单使用

    IOS学习:UITableView使用详解3 分组表的简单使用

    使用分组表和使用普通表的方法差不多,他们的不同点有以下几点:

    1.分组表的属性必须设置为Grouped的,而不是plain

    2.分组表的数据源方法当中numberOfSectionsInTableView:返回分组的个数。

    3.可以设置tableView:titleForHeaderInSection:返回每个分组的名称。

    4.可以使用方法sectionIndexTitlesForTableView:建立一个分组表的右侧索引,便于查找。

    以下通过一个案例进行分组表使用的讲解:

    新建一个工程使用sigleview,名称为GroupTableView,然后讲一个表视图拖放到视图控制器里面,将表视图的名称设置为Gouped,结果如图所示:

    在这里我们使用一个plist文件导入数据,里面显示了全国各个省以及每个省的城市,其结构如图所示:

    在头文件当中添加数据源和表视图委托,使用一个字典和一个数组,

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

    @property(copy,nonatomic)NSDictionary *dictionary;

    @propertyNSArray *province;

    @end

    加载视图后,读入数据源,因此在viewDidLoad方法当中添加一下代码:

    NSBundle *bundle=[NSBundlemainBundle];

    NSURL *plistURL=[bundle URLForResource:@"provinceCities"withExtension:@"plist"];

    dictionary =[NSDictionarydictionaryWithContentsOfURL:plistURL];

    province = [dictionaryallKeys];

    将数据从plist文件当中读入字典结构,它的key就是各个省份的名称,省份下的城市的名称组成的数组就是key的value。

    调用allKeys方法将所有的省份名称存入province数组,便于使用。

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

    {

        NSString *str=[provinceobjectAtIndex:section];

        NSArray *temp=[dictionaryobjectForKey:str];

        return [temp count];

    }

    该委托方法先使用section查找出要找的组的下标,使用它在province数组当中找出相应的名称,然后根据这个名称找出对应的城市名称数组,返回该数组的长度。

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return [provincecount];

    }

    返回分组的组的数目。

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

    {

        staticNSString *identifier=@"identifier";

        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

        if(cell==nil)

        {

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

        }

        int row=[indexPath row];

        int section=[indexPath section];

        NSString *pro=[provinceobjectAtIndex:section];

        cell.textLabel.text=[[dictionaryobjectForKey:pro] objectAtIndex:row];

        return cell;

    }

    该方法返回各个单元格。

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    {

        return [provinceobjectAtIndex:section];

    }

    该方法返回每个分组的标题

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

    {

        returnprovince;

    }

    返回用于建立索引的数组。

    效果如图所示

  • 相关阅读:
    java
    DevExpress学习1
    D 动态获取服务器访问路径
    D10 Sping Boot 入门 Sping框架--Java Web之文件操作--上传和下载
    D08 Sping Boot 入门 Sping框架--Java Web之JSP
    D07 Sping Boot 入门 Sping框架--Java Web之书城项目(二)实现用户登录功能
    D06 Sping Boot 入门 Sping框架--Java Web之书城项目(二)实现用户注册功能
    D04 Sping Boot 入门 Sping框架--Java Web之JavaEE的三层架构
    D03 Sping Boot 入门 Sping框架--Java Web之HTTP协议
    D02 Sping Boot 入门 Sping框架--Java Web之Servlet
  • 原文地址:https://www.cnblogs.com/jackwuyongxing/p/3519712.html
Copyright © 2011-2022 走看看