zoukankan      html  css  js  c++  java
  • UI基础 UITableView分区

    root.h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface RootViewController : UIViewController
    
    @end
    
    NS_ASSUME_NONNULL_END

    root.m

    #import "RootViewController.h"
    #import "Movie.h"
    @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        NSMutableArray *array;
        
    }
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UITableView *tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667-64) style:UITableViewStylePlain];
        [self.view addSubview:tab];
        
        tab.delegate=self;
        tab.dataSource=self;
        
        //添加表头
        UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 80)];
        imageV.image = [UIImage imageNamed:@"222"];
        tab.tableFooterView=imageV;
        
        //数据源数组
    //    array =[NSArray arrayWithObjects:@"亚瑟",@"剑圣",@"盲僧",@"老布",@"老牛", nil];
        array =[NSMutableArray array]; //初始化
        Movie* m1=[[Movie alloc]init];
        m1.movieName=@"";
        m1.movieActor=@"张大大";
        
        Movie* m2=[[Movie alloc]init];
        m2.movieName=@"";
        m2.movieActor=@"张大大";
        
        //将电影添加到数组中
        [array addObject:m1];
        [array addObject:m2];
        
        
        
    
    
    }
    
    //UITableView 代理方法
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //1.创建静态标识符
        static NSString *identifier =@"cell";
        //2.根据标识符从重用池中取cell
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        //3.如果没有取到就创建一个新的
        if (cell==nil){
            NSLog(@"进来了");
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        
        }
        //对cell进行赋值
        
        
    //    cell.textLabel.text =[array objectAtIndex:indexPath.row];
            
    //    cell.textLabel.text=@"老罗";
    //    cell.detailTextLabel.text=@"老罗这个人很屌";
        
        //这样写就很好
        
        Movie* tempMovie=[array objectAtIndex:indexPath.row];
        cell.textLabel.text=tempMovie.movieName;
        cell.detailTextLabel.text=tempMovie.movieActor;
        
        cell.imageView.image=[UIImage imageNamed:@"tianmao"];
        
        cell.accessoryType=UITableViewCellAccessoryDetailButton;
        
    //    cell.accessoryView=
        
        
        return cell;
        
        
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return array.count;
    }
    
    //cell的高度
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 60;
    }
    
    
    //控制分区的代理方法(亚州 ,欧美 ,日韩 )
    //分区的个数
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 3;
    }
    //分区的高度 控制高度
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        
        return 60;
    }
    
    //分区标题
    
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if(section==0){
            return @"亚洲";
        }else if(section==1){
            return @"欧美";
        }else{
            return @"日韩";
        }
        
    }
    
    
    //自定义分区
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
        
        view.backgroundColor=[UIColor redColor];
        return view;
        
    }

    movie.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface Movie : NSObject
    
    
    @property(nonatomic,strong)NSString* movieName;
    @property(nonatomic,strong)NSString* movieActor;
    
    
    @end
  • 相关阅读:
    Registering a Visual Studio .Net AddIn without an Installer
    广州的.Net Fans俱乐部的T恤和Logo的设计
    模板控件概念(转)
    随笔-在广州.Net俱乐部的一个下午
    快速备份Outlook中的资料
    web.config文件详解
    使用RAPI库操作移动设备——C#语言描述 (转贴)
    Ready to Rock 撼动未来:SQL Server 2005 ,Visual Studio 2005, BizTalk Server 2006 新品发布会 报名
    IBM Rational Solutions V2003
    VS.Net 2003 IDE 插件开发指南
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13473068.html
Copyright © 2011-2022 走看看