• 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
  • 相关阅读:
    156
    More Effective C++ 条款24 了解virtual function,multiple inheritance,virtual base classes,runtime type identification的成本
    More Effective C++ 条款23 考虑使用其他程序库
    More Effective C++ 条款22 考虑以操作符复合形式(op=)取代其独身形式(op)
    More Effective C++ 条款21 利用重载技术避免隐式类型转换
    More Effective C++ 条款20 协助完成"返回值优化(RVO)"
    More Effective C++ 条款19 了解临时对象的来源
    More Effective C++ 条款18 分期摊还预期的成本
    More Effective C++ 条款17 考虑使用lazy evaluation(缓式评估)
    More Effective C++ 条款16 谨记80-20法则
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13473068.html
Copyright © 2020-2023  润新知