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