zoukankan      html  css  js  c++  java
  • ios-表视图-demo2

    //
    //  RootViewController.m
    //  uitableview
    //
    //  Created by  liyang on 14-4-27.
    //  Copyright (c) 2014年 liyang. All rights reserved.
    //
    
    #import "RootViewController.h"
    #define KDeviceHeight [UIScreen mainScreen].bounds.size.height
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
           self.title=@"UITableViewStyle";
        }
        return self;
    }
    -(void)loadView{
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(200, 80, 3000, KDeviceHeight-20-44)];//这里随便设置最终都会再排版的,排版来适合屏幕,当有视图控制器的时候会根据视图控制器的bar来排版,(最终的理解,这些bar是没有盖住这个view的,只是合适的调整view的大小来适应屏幕,那么下面那个所谓的调整坐标系就是错误的)
        view.backgroundColor=[UIColor redColor];
        self.view=view;
        _fontarry=@[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];
        _uitableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, KDeviceHeight-20-44) style:UITableViewStylePlain];
        //_uitableview的frame设置的0,0,当这个viewcontroller的视图控制器是放再导航控制器里面,0,0自然是抛除了导航开始的,当特殊情况我们将导航控制器的navigationbar给隐藏了,那么0.0就是从视图控制器的view开始的,也就是说视图控制器是否在导航控制器中将直接影响其子视图的坐标系,原点坐标是不一样的
        
        _uitableview.dataSource=self;//设置数据源代理
        _uitableview.delegate=self;
        _uitableview.rowHeight=45;//行高,默认的44
        UIImageView*imageview=  [[UIImageView alloc]initWithFrame:self.view.bounds];
        imageview.image=[UIImage imageNamed:@"IMG_0410"];//设置背景图片
        _uitableview.backgroundView=imageview;
        
       [self.view addSubview:_uitableview];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [_fontarry count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString *cellindentifier=@"cellidentifier";
        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellindentifier];
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier];
        }
        NSString *fontname=_fontarry[indexPath.row];
        cell.textLabel.text=fontname;
        return cell;
    
    }
    #pragma tableview delegate
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathP{
     DetailViewController *detail=   [[DetailViewController alloc]init];
        detail.isPlan=indexPathP.row?NO:YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"didDeselectRowAtIndexPath%@",indexPath);
    
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    
    
    @end
    //
    //  DetailViewController.m
    //  uitableview
    //
    //  Created by  liyang on 14-4-27.
    //  Copyright (c) 2014年 liyang. All rights reserved.
    //
    
    #import "DetailViewController.h"
    #define KDeviceHeight [UIScreen mainScreen].bounds.size.height
    
    @interface DetailViewController ()
    
    @end
    
    @implementation DetailViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            self.title=@"detailviewcontroller";
        }
        return self;
    }
    -(void)loadView{
        UIView *view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        self.view=view;
        NSArray *array=[UIFont familyNames];
        _fontarry=[[NSMutableArray alloc]initWithCapacity:13];
        NSMutableArray *temparray=nil;
        
        for (int index=0; index<[array count]; index++) {
            NSString *font=array[index];
            if (index%5==0) {
                temparray=[[NSMutableArray alloc]initWithCapacity:5];
                [_fontarry addObject:temparray];//这里咋一看,可能以为是错的,因为初始化了一个空的来放进去,其实呢,外面又用了一个临时指针在往里面放东西
            }
            [temparray addObject:font];
        }
        UITableViewStyle style=self.isPlan?UITableViewStylePlain:UITableViewStyleGrouped;
        _uitableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, KDeviceHeight-20-44) style:style];
        
        _uitableview.dataSource=self;//设置数据源代理
        _uitableview.delegate=self;
        _uitableview.rowHeight=45;//行高,默认的44
        UIImageView*imageview=  [[UIImageView alloc]initWithFrame:self.view.bounds];
        imageview.image=[UIImage imageNamed:@"IMG_0410"];//设置背景图片
        _uitableview.backgroundView=imageview;
        
        [self.view addSubview:_uitableview];}
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
       }
    
    
    #pragma mark-uitableviewdatasource
    #pragma mark-uitableviewdelegate
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [_fontarry count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [_fontarry[section] count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString *cellindentifier=@"cellidentifier";
        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellindentifier];
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier];
        }
        NSString *fontname=_fontarry[indexPath.section][indexPath.row];
        cell.textLabel.text=fontname;
        cell.textLabel.font=[UIFont fontWithName:fontname size:14];
        return cell;
        
    }
    #pragma tableview delegate
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathP{
        DetailViewController *detail=   [[DetailViewController alloc]init];
         detail.isPlan=indexPathP.row?NO:YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"didDeselectRowAtIndexPath%@",indexPath);
        
    }
    //设置sectiontitle
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return [NSString stringWithFormat:@"第%d个section开始",section];
    }
    //- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    //return [NSString stringWithFormat:@"第%d个section结束",section];
    //}
    //设置行高
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 44;
    }
    //设置区域头视图高
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 55;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 50;
    }
    //自定义区域尾视图
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;{
        
       UIView *view= [[UIView alloc]initWithFrame:CGRectZero];
        view.backgroundColor=[UIColor purpleColor];
        return view;
    
    }
    //取消选中状态
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        NSIndexPath *indexpath= [_uitableview indexPathForSelectedRow];
        [_uitableview deselectRowAtIndexPath:indexpath animated:YES];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
       
    }
    
    
    
    @end
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    成功故事--甩手的故事 《转》
    从李小龙的一句话看程序员是否应该多学几种编程语言
    Windows编程中各种操作文件的方法《转载》
    MFC 属性表单的创建
    MFC 七种数据交换方式--对话框
    如何下载小众电影
    阿里2015前端笔试题
    jquery 20行代码实现简单轮播效果
    CSS定位与浮动
    盒子模型
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3694991.html
Copyright © 2011-2022 走看看