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];
    
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM