zoukankan      html  css  js  c++  java
  • uitableview分组的数据2中方式

    //
    //  ViewController.m
    //  tableviegroup
    //
    //  Created by ganchaobo on 13-7-2.
    //  Copyright (c) 2013年 ganchaobo. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Person.h"
    @interface ViewController ()
    @property(nonatomic,retain)NSMutableArray *mydata;
    @end
    
    @implementation ViewController
    
    #pragma mark -生命周期方法
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        tableview.dataSource=self;
        tableview.delegate=self;
        [self.view addSubview:tableview];
        [tableview release];
        self.mydata=[NSMutableArray array];
        [self InitData1];
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)viewDidUnload{
        [super viewDidUnload];
        self.mydata=nil;
    }
    
    - (void)dealloc
    {
        self.mydata=nil;
        [super dealloc];
    }
    //数据类--》数组--(包含数组)--(每一个数组中只包含多个字典(并且每个字典只有一个key--value)
    
    #pragma mark -初始化数据
    
    -(void)InitData1{
        //最外面的数组
        for (int i=0; i<2; i++) {
            NSMutableArray *arr1=[NSMutableArray array];
            for (int j=0; j<3; j++) {
                NSString *key=[NSString stringWithFormat:@"itcast-->%zi",j];
                NSDictionary *dic=@{key:@(i)};
                [arr1 addObject:dic];
                
            }
            [self.mydata addObject:arr1];
        }
        
    }
    
    第一种是字典
    
    //一般数据和控件没有太大直接关系
    #pragma mark -uitableview 代理方法
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.mydata.count;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       NSArray *sectionq= self.mydata[section];
        return sectionq.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndetify=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndetify];
        if(cell==nil){
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify];
            
        }
        NSArray *section=self.mydata[indexPath.section];
        NSDictionary *row=section[indexPath.row];
       
        cell.textLabel.text=row.allKeys[0] ;
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%zi",[row.allValues[0] intValue]];
        return  cell;
    }
    
    @end

    第二种模型

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    @property(nonatomic,copy)NSString *name;
    @property(nonatomic,copy)NSString *dec;
    @end
    
    
    
    
    //
    //  ViewController.m
    //  tableviegroup
    //
    //  Created by ganchaobo on 13-7-2.
    //  Copyright (c) 2013年 ganchaobo. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Person.h"
    @interface ViewController ()
    @property(nonatomic,retain)NSMutableArray *mydata;
    @end
    
    @implementation ViewController
    
    #pragma mark -生命周期方法
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        tableview.dataSource=self;
        tableview.delegate=self;
        [self.view addSubview:tableview];
        [tableview release];
        self.mydata=[NSMutableArray array];
        [self InitData2];
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)viewDidUnload{
        [super viewDidUnload];
        self.mydata=nil;
    }
    
    - (void)dealloc
    {
        self.mydata=nil;
        [super dealloc];
    }
    
    
    -(void)InitData2{
        //最外面的数组
        for (int i=0; i<2; i++) {
            NSMutableArray *arr1=[NSMutableArray array];
            for (int j=0; j<3; j++) {
                Person *ps=[[Person alloc] init];
                ps.name=[NSString stringWithFormat:@"itcast-->%zi",j];
                ps.dec=[NSString stringWithFormat:@"%zi",j];
                [arr1 addObject:ps];
                [ps release];
                
            }
            [self.mydata addObject:arr1];
        }
        
    }
    
    
    //一般数据和控件没有太大直接关系
    #pragma mark -uitableview 代理方法
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.mydata.count;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       NSArray *sectionq= self.mydata[section];
        return sectionq.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndetify=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndetify];
        if(cell==nil){
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify];
            
        }
        NSArray *section=self.mydata[indexPath.section];
        Person *row=section[indexPath.row];
       
        cell.textLabel.text=row.name;
        cell.detailTextLabel.text=row.dec;
        return cell;
    }
    
    @end
  • 相关阅读:
    elasticsearch 索引清理脚本及常用命令
    git 快速入门及常见用法
    第01章-成本,你真的算对过吗?
    windows mysql安装及常用命令
    centos7 systemctl配置开机自启动服务
    python pip手动安装二进制包
    centos7使用nginx+uwsgi部署python django项目
    python json.loads()、json.dumps()和json.dump()、json.load()区别
    比阿里云快2倍的InfluxDB集群,我们开源了
    为什么是InfluxDB | 写在《InfluxDB原理和实战》出版之际
  • 原文地址:https://www.cnblogs.com/gcb999/p/3168498.html
Copyright © 2011-2022 走看看