UI总结-自定义cell

           UI总结-自定义cell

      在我们实际编程中很少能用到系统的cell,这时候我们就要写自己的cell,下面的详细的编码过程

ViewController.m文件:

#import "ViewController.h"

#import "MovieCell.h"

@interface ViewController ()

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *arr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

[self creatData];

self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

[self.view addSubview:self.tableView];

[_tableView release];

self.tableView.rowHeight = 150;

self.tableView.dataSource = self;

self.tableView.delegate = self;

}

//从本地获取文件

-(void)creatData{

NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];

self.arr = [NSMutableArray arrayWithContentsOfFile:path];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.arr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *movie = @"movie";

MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:movie];

if (!cell) {

cell = [[MovieCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:movie];

}

//给cell赋值

cell.movieName.text = [self.arr[indexPath.row] valueForKey:@"name"];

return cell;

}

cell.h文件,把你要在cell显示的试图都写成属性,方便调用:

#import

@interface MovieCell : UITableViewCell

@property(nonatomic, retain)UIImageView *movieImage ;

@property(nonatomic, retain)UILabel *movieName;

@property(nonatomic, retain)UIView *diView;

@end

cell.m文件:

#import "MovieCell.h"

#define WIDTH self.contentView.frame.size.width

#define HEIGHT self.contentView.frame.size.height

@implementation MovieCell

//初始化方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

[self creatView];

}

return self;

}

//在cell上创建视图

//要记得属性名不能和系统cell的3个控件名重名

//在创建的时候一般不写坐标,尺寸

-(void)creatView{

self.diView = [[UIView alloc]init];

self.diView.backgroundColor = [UIColor grayColor];

[self.contentView addSubview:self.diView];

[_diView release];

self.movieName = [[UILabel alloc]init];

[self.contentView addSubview:self.movieName];

self.movieName.backgroundColor = [UIColor redColor];

[_movieName release];

self.movieImage = [[UIImageView alloc]init];

[self.contentView addSubview:self.movieImage];

self.movieImage.backgroundColor = [UIColor brownColor];

[_movieImage release];

}

//这个方法是tableview显示之前执行的最后的方法,所以在执行这个方法的时候控件已经完成了赋值,在这个方法里设置控件的尺寸.

-(void)layoutSubviews{

//下面的这句话一定要写,不写的会影响cell的尺寸(我在没写的情况下,在cell中出现了tableView分割线)

[super layoutSubviews];

self.diView.frame = CGRectMake(10, 5, WIDTH - 20, HEIGHT - 10);

self.movieImage.frame = CGRectMake(10, 5, WIDTH / 3, HEIGHT - 10);

self.movieName.frame = CGRectMake(WIDTH / 3  + 20, 5, WIDTH / 3 - 20, HEIGHT - 10);

}

运行结果如下:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,093评论 3 38
  • 使用过Excel的同志应该都会经历被Excel里面的粘贴选项所迷惑,几种粘贴的选项的效果可能都不是很明确,尤其是面...
    雪呢阅读 2,588评论 2 1
  • 窗外的雪越飘越大,早晨起床后还沉浸在昨晚回家了的梦境中,写下这篇想回家过年的文章,越在这个时候,回家的心越来越急切...
    丢丢吧阅读 748评论 6 9
  • 也不记得是哪年哪月哪日,看见总裁在群里不成功地安利suits,贴了剧照,双男主角,颜值中等偏上(我的审美标准是很高...
    双眼皮面瘫阅读 863评论 0 1