快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

自定义单元格-创新互联

自定义单元格:三种方法

成都创新互联于2013年创立,先为内江等服务建站,内江等地企业,进行企业商务咨询服务。为内江企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

方法一:向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];

  //内容下移64px

  tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

  tableView.dataSource = self;

  tableView.delegate = self;

  //获取数据

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

    //注意:控件的创建应该跟在tableVC的初始化放在一起,确保tableVC当中只有自己创建的这一个控件,不会出现空间的叠加(共有)

    //添加图片

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

    //添加电影评分

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

  }

  //对控件赋值应该放在外面(特有)

  //图片

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

  //评分

  UILabel *ratingLabel = (UILabel *)[tableVC.contentView viewWithTag:102];

  ratingLabel.text = [NSString stringWithFormat:@"评分:%@",[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

方法二:先创建一个xib文件---(然后在xib上拖一个tableCell并设置大小---(在tableCell上拖需要的控件并设置控件的属性和tag值-----(将xib文件中tableCell的identifier该为设置的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;

  //获取数据源

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

  //评分

  UILabel *ratingLabel = (UILabel *)[tableVC.contentView viewWithTag:102];

  ratingLabel.text = [NSString stringWithFormat:@"评分:%@",[dic objectForKey:@"rating"]];

  //年份

  UILabel *yearLabel = (UILabel *)[tableVC.contentView viewWithTag:103];

  yearLabel.text = [NSString stringWithFormat:@"年份:%@",[dic objectForKey:@"year"]];

  return tableVC;

}

@end

方法三:

(1)方法一:在storyboard文件中拖一个tableView,然后再拖一个tableCell并设置大小---(在tableCell上拖需要的控件并设置控件的属性-----(建立一个类继承于UITableViewCell,将各个控件在此文件中声明并声明一个字典(根据情况而定)接收ViewContoller中值-------(将storyboard文件中tableCell的identifier该为设置的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];

  //在控制器中,不应该设置太多视图自己需要显示的内容,控制器充当MVC架构模式中的C,需要做的应该啊hi把M------->V

  //M(Model)

  NSDictionary *dic = self.dataArray[indexPath.row];

  //dic-------->Cell

  tableVC.dataDic = dic;

  return tableVC;

}

@end

#import "MovieTableViewCell.h"

@implementation MovieTableViewCell

//当视图从xib文件或者storyboard中加载时,走这个方法,相当于初始化方法

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

    //此时确保值能传过来

    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:@"评分:%@",[self.dataDic objectForKey:@"rating"]];

    self.yearLabel.text = [NSString stringWithFormat:@"年份:%@",[self.dataDic objectForKey:@"year"]];

  }

}

@end

(2)(此方法比较常用)方法二:建立一个类继承于UITableViewCell和创建一个类MoviewModel继承与NSObject-------(将各个控件在继承于UITableViewCell文件中创建并声明一个MovieModel接收-----(在ViewContoller文件中创建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;

  //获取数据源

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

    //将数据填充到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;

//     //手动调动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];

  //添加电影评分

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

  }

}

//当子视图重新布局时需要调用的方法

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

  //评分

  UILabel *ratingLabel = (UILabel *)[self.contentView viewWithTag:102];

//   ratingLabel.text = [NSString stringWithFormat:@"评分:%@",[self.dataDic objectForKey:@"rating"]];

  ratingLabel.text = [NSString stringWithFormat:@"评分:%@",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

@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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:自定义单元格-创新互联
网页URL:http://6mz.cn/article/cdjgcc.html

其他资讯