自定義單元格-創(chuàng)新互聯(lián)

自定義單元格:三種方法

成都創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為內(nèi)江等服務(wù)建站,內(nèi)江等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為內(nèi)江企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

方法一:向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,并繼承于新建的文件。

主要代碼寫在聲明的字典的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í)確保值能傳過來

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

  //由原來的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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

新聞標(biāo)題:自定義單元格-創(chuàng)新互聯(lián)
瀏覽地址:http://www.bm7419.com/article22/cdjgcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、軟件開發(fā)建站公司、企業(yè)網(wǎng)站制作品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作