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];
    
    
}
原文地址:https://www.cnblogs.com/zhangqing979797/p/13620254.html