zoukankan      html  css  js  c++  java
  • UI09_UITableView 使用 单例

    ========—————==========————=============———
    //
    //  AppDelegate.m
    //  UI09_UITableView
    //  Created by dllo on 15/10/28.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //

    #import "AppDelegate.h"
    #import "RootViewController.h"
    @interface AppDelegate ()

    @end

    @implementation AppDelegate


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
       
        RootViewController *rootVC = [[RootViewController alloc]init];
        UINavigationController *naviC = [[UINavigationController alloc]initWithRootViewController:rootVC];
        self.window.rootViewController = naviC;
     
        return YES;
    }
    @end
    //
    //  RootViewController.m
    //  UI09_UITableView
    //  Created by dllo on 15/10/28.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //#import "RootViewController.h"
    #import "FirstViewController.h"
    #import "Singleton.h"
    #import "Student.h"
    @interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

    @end

    @implementation RootViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor orangeColor];
       
        UITableView *myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];// 分行的
    //    UITableViewStyleGrouped 分组的
        myTableView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:myTableView];
       
        myTableView.delegate = self;// 基本配置代理
        myTableView.dataSource = self;// 数据源代理
       
        // 去掉cell的线
        myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
        headerView.backgroundColor = [UIColor colorWithRed:1.000 green:0.252 blue:0.457 alpha:1.000];
        // 设置tableView的表头视图
        myTableView.tableHeaderView = headerView;
       
    //    [self setAutomaticallyAdjustsScrollViewInsets:NO];
       
        // 刷新整个tableview
        [myTableView reloadData];
        // 刷新指定的cell
    //    [myTableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
       
    //    [Singleton shareSingleton];
       
     
    }
    // 设置cell行高
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 100;
    }

    // 设置Section的个数 区的个数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [Singleton shareSingleton].studentDic.allKeys.count;
    }
    // 设置每个Section的行数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:section];    // 根据key取出对应的数组
        NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
        // 当前Section的行数由数组个数决定
        return array.count;
    }
    //
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
            //1. 先从重用池里取
        // queue:队列  reusable:重用  identifier:标识符
        // 标识符是区别不同Cell的唯一标记
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            //2. Cell是否为空,为空则alloc

        if (cell == nil) {// 对象可以直接==
    //        Cell是否为空,为空则创建
    //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];// style  类型  自己试试
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];// 副标题
         
        }
        //**  注意执行顺序  重用机制  显示最后的代码
        NSString *string = [NSString stringWithFormat:@"section = %ld,row = %ld",indexPath.section,indexPath.row];
        cell.textLabel.text = string;
        if ((indexPath.section == 1) && (indexPath.row == 2)) {
            cell.textLabel.text = @"第三步";
        }
        // 副标题
        cell.detailTextLabel.text = @"副标题";
        //设置cell显示图片
        cell.imageView.image = [UIImage imageNamed:@""];
        // 取消cell的置灰效果
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
        NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:indexPath.section];
        NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
        Student *stu = [array objectAtIndex:indexPath.row];
    //    cell.textLabel.text = stu.name;

        //举例刷新(随机数)
        NSString *string1 = [NSString stringWithFormat:@"%d",arc4random()%201];
            cell.textLabel.text = string1;
        return cell;
    }
    #pragma mark -- 设置字母检索
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    //    return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", nil];
        return [Singleton shareSingleton].studentDic.allKeys;
    }
    // 设置Section表头标题
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        /*if (section == 0) {
            return @"A";
        }
        if (section == 1) {
            return @"B";
        }
        if (section == 2) {
            return @"C";
        }
        return @"D";*/
        NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:section];
       
        return key;
    }
    // 选中cell触发的方法
    // didDeselectRowAtIndexPath 选中的时候不触发,选中下一个cell时触发
    // didSelectRowAtIndexPath 选中cell时触发
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        /*
        // 选中的时候有置灰效果,离开时消失选中效果
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
       
        // 自定义cell下标位置路径
        NSIndexPath *indexPt = [NSIndexPath indexPathForRow:2 inSection:2];
        // 滚动到某一行cell
        [tableView scrollToRowAtIndexPath:indexPt atScrollPosition:UITableViewScrollPositionBottom animated:YES];
       
        NSLog(@"section = %ld row = %ld",indexPath.section, indexPath.row);
        FirstViewController *firstVC = [[FirstViewController alloc]init];
        NSString *string1 = [NSString stringWithFormat:@"section = %ld,row = %ld",indexPath.section,indexPath.row];
        firstVC.string = string1;
    //     firstVC.indexPath = self.indexPath;
        [self.navigationController pushViewController: firstVC animated:YES];
        */
       
        // 刷新全部数据
    //    [tableView reloadData];

        // 刷新指定的cell,可以是多行
        // 创建指定的某行的下标对象
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
       
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,index, nil] withRowAnimation:UITableViewRowAnimationBottom];//indexPath 点击的 当前的Row
    }
     
    @end
    ========—————==========————===========
    //
    //  FirstViewController.h
    //  UI09_UITableView
     //  Created by dllo on 15/10/28.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //
    #import <UIKit/UIKit.h>

    @interface FirstViewController : UIViewController

    @property (nonatomic,retain)NSString *string;

    //@property (nonatomic, retain)NSInteger indexPath;
    @end
     
     
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.853 blue:0.000 alpha:1.000];
       
       
        self.navigationItem.title = self.string;
       
    //    self.title = [NSString stringWithFormat:@"%d %d",self.indexPath.section, self.indexPath.row];
       
    }

    @end
    //
    //  Singleton.h
    //  UI09_UITableView
    //  Created by dllo on 15/10/28.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //
    #import <Foundation/Foundation.h>

    @interface Singleton : NSObject

    + (instancetype)shareSingleton;


    @property (nonatomic, retain)NSMutableDictionary *studentDic;
     
    @end
    //
    //  Singleton.m
    //  UI09_UITableView
    //  Created by dllo on 15/10/28.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //
    #import "Singleton.h"
    #import "Student.h"

    @implementation Singleton

    #warning 问题  为什么不能调用-号方法
    + (instancetype)shareSingleton{
        static Singleton *singleton = nil;
        if (singleton == nil) {
            // if里面只执行一次
            singleton = [[Singleton alloc]init];
            // self 在+号方法里代表当前的类
            [singleton createStudentDic];
        }
        return singleton;
    }


    - (void)createStudentDic{
        // 张啊
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"张啊", @"name", @"男", @"gender", nil];
        // 王婷
        NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"王婷", @"name", @"女", @"gender", nil];
        // 张前
    //    NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"张前", @"name", @"男", @"gender", nil];
        NSMutableArray *zhangArray = [NSMutableArray arrayWithObjects:dic1,dic2, nil];
        // 王天
        NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"王天", @"name", @"男", @"gender", nil];
        // 王飞
        NSMutableDictionary *dic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"王飞", @"name", @"男", @"gender", nil];
        NSMutableArray *wangArray = [NSMutableArray arrayWithObjects:dic3, dic4, nil];
       
        // 大字典
        NSMutableDictionary *bigDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:zhangArray,@"Z",wangArray,@"W", nil];
        NSLog(@"bigDic = %@",bigDic);
       
        // 取两个以上的 要用遍历 for循环
        // model
        // 将大字典扒开,取到小字典里学生的信息,并赋值给model对象
       
        // 创建最外层大字典
        self.studentDic = [NSMutableDictionary dictionary];
       
        for (NSString *Key in bigDic) {
            // 根据key取出对应的数组
            NSMutableArray *array = [bigDic objectForKey:Key];
           
            // 创建一个新的数组,存model对象
            NSMutableArray *stuArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in array) {
                // 一个学生对象对应一个学生小字典
                Student *stu = [[Student alloc]init];
                stu.name = [dic objectForKey:@"name"];
                stu.gender = [dic objectForKey:@"gender"];
               
                // 将model对象存入数组中
                [stuArray addObject:stu];
                [stu release];     
            }
           
            [self.studentDic setObject:stuArray forKey:Key];
        }
       
    }
    @end
    //
    //  Student.h
    //  UI09_UITableView
    //
    //  Created by dllo on 15/10/21.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    @interface Student : NSObject

    @property (nonatomic,copy)NSString *name;
    @property (nonatomic,copy)NSString *gender;
     
    @end
     
    //  Student.m
    //  UI09_UITableView
     //
    //  Created by dllo on 15/10/21.
    //  Copyright (c) 2015年 z_han. All rights reserved.
    //
    #import "Student.h"

    @implementation Student

    @end
  • 相关阅读:
    时间格式
    分页1
    vs2010 VS2008 VS2005 快捷键大全
    css 常用标签
    JS Array数组操作
    CSS属性
    jquery 选择器大全
    @fontface
    以前写过的ajax基础案例(王欢huanhuan)
    Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢)
  • 原文地址:https://www.cnblogs.com/z-han49888/p/4951034.html
Copyright © 2011-2022 走看看