zoukankan      html  css  js  c++  java
  • UI基础 UITableView 自定义cell

    app.m

    #import "RootViewController.h"
    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        self.window.rootViewController = [[UIViewController alloc]init];
        [self.window makeKeyAndVisible];
        
        RootViewController * root =[[RootViewController alloc]init];
        UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:root];
        
        nav.navigationBar.barTintColor=[UIColor greenColor];
        nav.navigationBar.translucent=NO;
        
        self.window.rootViewController=nav;
        
        
        

    root.m

    #import "RootViewController.h"
    #import "MyTableViewCell.h"
    @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
    
    @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;
    
    }
    
    //UITableView 代理方法
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //1.创建静态标识符
        static NSString *identifier =@"cell";
        //2.根据标识符从重用池中取cell
        MyTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        //3.如果没有取到就创建一个新的
        if (cell==nil){
            NSLog(@"进来了");
            cell=[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        
        }
        // 调用自己写的控件
        cell.movieName.text=@"湄公河";
            
       
        return cell;
        
        
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 5;
    }
    
    //cell的高度
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        return 100;
        
    }
    
    
    @end

    自定义 cell.h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MyTableViewCell : UITableViewCell
    
    //第一步 定义需要的属性
    @property(nonatomic,strong)UIImageView *movieIcon;
    @property(nonatomic,strong)UILabel *movieName;
    @property(nonatomic,strong)UILabel *movieActor;
    @property(nonatomic,strong)UILabel *movieTime;
    
    @end
    
    
    NS_ASSUME_NONNULL_END

    cell.m

    #import "MyTableViewCell.h"
    
    @implementation MyTableViewCell
    
    //重写初始化方法
    -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
        
        if (self){
            //在初始化方法中完成初始化 并添加到cell上
            self.movieIcon=[[UIImageView alloc] init];
            [self.contentView addSubview:self.movieIcon];
            
            self.movieName=[[UILabel alloc] init];
            [self.contentView addSubview:self.movieName];
            
            self.movieActor=[[UILabel alloc] init];
            [self.contentView addSubview:self.movieActor];
            
            self.movieTime=[[UILabel alloc] init];
            [self.contentView addSubview:self.movieTime];
            
        }
        return self;
    }
    
    //第三步 设置控件信息
    -(void)layoutSubviews
    {
        
        [super layoutSubviews];
        
        self.movieIcon.frame =CGRectMake(10, 10, 50, 50);
        self.movieName.frame=CGRectMake(70, 10, 260, 20);
        self.movieActor.frame=CGRectMake(70, 40, 260, 20);
        self.movieTime.frame=CGRectMake(70, 70, 260, 20);
        
        self.movieIcon.backgroundColor =[UIColor redColor];
        self.movieName.backgroundColor =[UIColor greenColor];
        self.movieActor.backgroundColor =[UIColor greenColor];
        self.movieTime.backgroundColor =[UIColor greenColor];
        
        
    }
  • 相关阅读:
    @Schedule注解中的Cron表达式读取properties的方法
    antdv 使用单文件方式递归生成菜单
    git 新建分支并将代码推送到远程
    echarts 饼图 pie label 颜色自定义
    关于bootstrapValidator 表单校验remote出现两次重复提交才能验证通过问题处理
    bootstrap table实现x-editable的行单元格编辑及解决数据Empty的问题
    element-ui 弹出组件的遮罩层在弹出层dialog模态框的上面
    vue cli 3.0 用axios 调用本地json数据一直报404
    vue项目webpack打包部署到tomcat时,访问成功,但是刷新后页面报404
    select2的使用
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13620254.html
Copyright © 2011-2022 走看看