您好,登錄后才能下訂單哦!
自定義單元格:三種方法
方法一:向contentView添加子視圖
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//內(nèi)容下移64px
tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
tableView.dataSource = self;
tableView.delegate = self;
//獲取數(shù)據(jù)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Films" ofType:@"plist"];
self.dataArray = [[NSArray alloc] initWithContentsOfFile:filePath];
[self.view addSubview:tableView];
}
#pragma Mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
NSString *identifier = @"MyCell";
UITableViewCell *tableVC = [tableView dequeueReusableCellWithIdentifier:identifier];
if (tableVC == nil) {
tableVC = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//注意:控件的創(chuàng)建應(yīng)該跟在tableVC的初始化放在一起,確保tableVC當(dāng)中只有自己創(chuàng)建的這一個(gè)控件,不會(huì)出現(xiàn)空間的疊加(共有)
//添加圖片
UIImageView *p_w_picpathView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 150, 180)];
p_w_picpathView.tag = 100;
[tableVC.contentView addSubview:p_w_picpathView];
//添加電影名稱
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 10, 200, 40)];
titleLabel.tag = 101;
titleLabel.font = [UIFont boldSystemFontOfSize:16];
titleLabel.textColor = [UIColor blueColor];
[tableVC.contentView addSubview:titleLabel];
//添加電影評(píng)分
UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 60, 100, 40)];
ratingLabel.tag = 102;
ratingLabel.font = [UIFont boldSystemFontOfSize:14];
ratingLabel.textColor = [UIColor cyanColor];
[tableVC.contentView addSubview:ratingLabel];
//添加電影的年份
UILabel *yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 110, 100, 20)];
yearLabel.tag = 103;
yearLabel.font = [UIFont boldSystemFontOfSize:12];
yearLabel.textColor = [UIColor orangeColor];
[tableVC.contentView addSubview:yearLabel];
}
//對(duì)控件賦值應(yīng)該放在外面(特有)
//圖片
UIImageView *p_w_picpathView = (UIImageView *)[tableVC.contentView viewWithTag:100];
p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:[dic objectForKey:@"p_w_picpath"]];
//名字
UILabel *titleLabel = (UILabel *)[tableVC.contentView viewWithTag:101];
titleLabel.text = [NSString stringWithFormat:@"電影:%@",[dic objectForKey:@"title"]];
//評(píng)分
UILabel *ratingLabel = (UILabel *)[tableVC.contentView viewWithTag:102];
ratingLabel.text = [NSString stringWithFormat:@"評(píng)分:%@",[dic objectForKey:@"rating"]];
//年份
UILabel *yearLabel = (UILabel *)[tableVC.contentView viewWithTag:103];
yearLabel.text = [NSString stringWithFormat:@"年份:%@",[dic objectForKey:@"year"]];
return tableVC;
}
#pragma Mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
@end
方法二:先創(chuàng)建一個(gè)xib文件---(然后在xib上拖一個(gè)tableCell并設(shè)置大小---(在tableCell上拖需要的控件并設(shè)置控件的屬性和tag值-----(將xib文件中tableCell的identifier該為設(shè)置的identifier
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
tableView.delegate =self;
tableView.dataSource = self;
[self.view addSubview:tableView];
//定制單元格的高度
tableView.rowHeight = 200;
//獲取數(shù)據(jù)源
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Films" ofType:@"plist"];
self.dataArray = [NSArray arrayWithContentsOfFile:filePath];
}
#pragma mark -UITableViewDataSource
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"MyCell";
UITableViewCell *tableVC = [tableView dequeueReusableCellWithIdentifier:identifier];
if (tableVC == nil) {
tableVC =[[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil]lastObject];
}
NSDictionary *dic = self.dataArray[indexPath.row];
//圖片
UIImageView *p_w_picpathView = (UIImageView *)[tableVC.contentView viewWithTag:100];
p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:[dic objectForKey:@"p_w_picpath"]];
//電影名
UILabel *titleLabel = (UILabel *)[tableVC.contentView viewWithTag:101];
titleLabel.text = [NSString stringWithFormat:@"電影:%@",[dic objectForKey:@"title"]];
//評(píng)分
UILabel *ratingLabel = (UILabel *)[tableVC.contentView viewWithTag:102];
ratingLabel.text = [NSString stringWithFormat:@"評(píng)分:%@",[dic objectForKey:@"rating"]];
//年份
UILabel *yearLabel = (UILabel *)[tableVC.contentView viewWithTag:103];
yearLabel.text = [NSString stringWithFormat:@"年份:%@",[dic objectForKey:@"year"]];
return tableVC;
}
@end
方法三:
(1)方法一:在storyboard文件中拖一個(gè)tableView,然后再拖一個(gè)tableCell并設(shè)置大小---(在tableCell上拖需要的控件并設(shè)置控件的屬性-----(建立一個(gè)類繼承于UITableViewCell,將各個(gè)控件在此文件中聲明并聲明一個(gè)字典(根據(jù)情況而定)接收ViewContoller中值-------(將storyboard文件中tableCell的identifier該為設(shè)置的identifier,并繼承于新建的文件。
主要代碼寫(xiě)在聲明的字典的set方法中。
#import "ViewController.h"
#import "MovieTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Films" ofType:@"plist"];
self.dataArray = [NSArray arrayWithContentsOfFile:filePath];
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//V(View)
MovieTableViewCell *tableVC = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
//在控制器中,不應(yīng)該設(shè)置太多視圖自己需要顯示的內(nèi)容,控制器充當(dāng)MVC架構(gòu)模式中的C,需要做的應(yīng)該啊hi把M------->V
//M(Model)
NSDictionary *dic = self.dataArray[indexPath.row];
//dic-------->Cell
tableVC.dataDic = dic;
return tableVC;
}
@end
#import "MovieTableViewCell.h"
@implementation MovieTableViewCell
//當(dāng)視圖從xib文件或者storyboard中加載時(shí),走這個(gè)方法,相當(dāng)于初始化方法
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)setDataDic:(NSDictionary *)dataDic
{
if (_dataDic != dataDic) {
_dataDic = dataDic;
//此時(shí)確保值能傳過(guò)來(lái)
self.imgView.p_w_picpath = [UIImage p_w_picpathNamed:[self.dataDic objectForKey:@"p_w_picpath"]];
self.titleLabel.text = [NSString stringWithFormat:@"電影:%@",[self.dataDic objectForKey:@"title"]];
self.ratingLabel.text = [NSString stringWithFormat:@"評(píng)分:%@",[self.dataDic objectForKey:@"rating"]];
self.yearLabel.text = [NSString stringWithFormat:@"年份:%@",[self.dataDic objectForKey:@"year"]];
}
}
@end
(2)(此方法比較常用)方法二:建立一個(gè)類繼承于UITableViewCell和創(chuàng)建一個(gè)類MoviewModel繼承與NSObject-------(將各個(gè)控件在繼承于UITableViewCell文件中創(chuàng)建并聲明一個(gè)MovieModel接收-----(在ViewContoller文件中創(chuàng)建MovieModel,接收值。
#import "ViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
tableView.delegate =self;
tableView.dataSource = self;
[self.view addSubview:tableView];
tableView.rowHeight = 200;
//獲取數(shù)據(jù)源
// NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Films" ofType:@"plist"];
// self.dataArray = [NSArray arrayWithContentsOfFile:filePath];
//由原來(lái)的dataDic改為裝movieModel
NSArray *dataArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Films" ofType:@"plist"]];
NSMutableArray *mArray = [NSMutableArray array];
for (NSDictionary *dic in dataArr) {
NSString *title = [dic objectForKey:@"title"];
NSString *p_w_picpathName = [dic objectForKey:@"p_w_picpath"];
NSString *rating = [dic objectForKey:@"rating"];
NSString *year = [dic objectForKey:@"year"];
//將數(shù)據(jù)填充到movieModel
MovieModel *model = [[MovieModel alloc] init];
model.title = title;
model.p_w_picpathName = p_w_picpathName;
model.ratingLabel = rating;
model.yearLabel = year;
[mArray addObject:model];
}
self.dataArray = mArray;
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"MyCell";
MovieTableViewCell *tableVC = [tableView dequeueReusableCellWithIdentifier:identifier];
if (tableVC == nil) {
tableVC = [[MovieTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// NSDictionary *dic = self.dataArray[indexPath.row];
// tableVC.dataDic = dic;
tableVC.movieModel = self.dataArray[indexPath.row];
return tableVC;
}
@end
#import "MovieTableViewCell.h"
@implementation MovieTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self != nil) {
//........
[self initViews];
}
return self;
}
- (void)awakeFromNib {
// Initialization code
[super awakeFromNib];
[self initViews];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//- (void)setDataDic:(NSDictionary *)dataDic
//{
// if (_dataDic != dataDic) {
// _dataDic = dataDic;
// //手動(dòng)調(diào)動(dòng)layoutSubviews
// [self setNeedsLayout];
// }
//}
//初始化自身的子視圖
- (void)initViews
{
//添加圖片
UIImageView *p_w_picpathView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 150, 180)];
p_w_picpathView.tag = 100;
[self.contentView addSubview:p_w_picpathView];
//添加電影名稱
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 10, 200, 40)];
titleLabel.tag = 101;
titleLabel.font = [UIFont boldSystemFontOfSize:16];
titleLabel.textColor = [UIColor blueColor];
[self.contentView addSubview:titleLabel];
//添加電影評(píng)分
UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 60, 100, 40)];
ratingLabel.tag = 102;
ratingLabel.font = [UIFont boldSystemFontOfSize:14];
ratingLabel.textColor = [UIColor cyanColor];
[self.contentView addSubview:ratingLabel];
//添加電影的年份
UILabel *yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 110, 100, 20)];
yearLabel.tag = 103;
yearLabel.font = [UIFont boldSystemFontOfSize:12];
yearLabel.textColor = [UIColor orangeColor];
[self.contentView addSubview:yearLabel];
}
-(void)setMovieModel:(MovieModel *)movieModel
{
if (_movieModel != movieModel) {
_movieModel = movieModel;
[self setNeedsLayout];
}
}
//當(dāng)子視圖重新布局時(shí)需要調(diào)用的方法
- (void)layoutSubviews
{
//一定注意(不可缺少)
[super layoutSubviews];
//圖片
UIImageView *p_w_picpathView = (UIImageView *)[self.contentView viewWithTag:100];
// p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:[self.dataDic objectForKey:@"p_w_picpath"]];
p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:self.movieModel.p_w_picpathName];
//名字
UILabel *titleLabel = (UILabel *)[self.contentView viewWithTag:101];
// titleLabel.text = [NSString stringWithFormat:@"電影:%@",[self.dataDic objectForKey:@"title"]];
titleLabel.text = [NSString stringWithFormat:@"電影:%@",self.movieModel.title];
//評(píng)分
UILabel *ratingLabel = (UILabel *)[self.contentView viewWithTag:102];
// ratingLabel.text = [NSString stringWithFormat:@"評(píng)分:%@",[self.dataDic objectForKey:@"rating"]];
ratingLabel.text = [NSString stringWithFormat:@"評(píng)分:%@",self.movieModel.ratingLabel];
//年份
UILabel *yearLabel = (UILabel *)[self.contentView viewWithTag:103];
// yearLabel.text = [NSString stringWithFormat:@"年份:%@",[self.dataDic objectForKey:@"year"]];
yearLabel.text = [NSString stringWithFormat:@"年份:%@",self.movieModel.yearLabel];
}
@end
MovieModel.h文件
#import <Foundation/Foundation.h>
@interface MovieModel : NSObject
@property (nonatomic,copy)NSString *p_w_picpathName;
@property (nonatomic,copy)NSString *title;
@property (nonatomic,copy)NSString *rating;
@property (nonatomic,copy)NSString *year;
@end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。