Viewcontroller.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> @property(nonatomic,retain)NSArray *textlist; //定义一个数组名为textlist @property(nonatomic,retain)NSArray *imagelist; //定义一个数组名为imagelist @property(nonatomic,retain)NSArray *detallist; //定义一个数组名为detallist @property(nonatomic,retain)NSArray *colorlist; //定义一个数组名为colorlist @property(nonatomic,retain)UITableView *myTableView; //定义一个UITableView名为myTableView @end
Viewcontroller.m // // ViewController.m // UItableViewCell // // Created by 李旗 on 15/11/5. // Copyright (c) 2015年李旗. All rights reserved. // #import "ViewController.h" #import "TableViewCell.h" @interfaceViewController () @end @implementation ViewController - (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *text = [NSArrayarrayWithObjects:@"中国",@"美国",@"韩国",@"英国", nil ]; self.textlist = text;//创建元素为国家的数组text NSArray *image = [NSArrayarrayWithObjects:@"yuan.jpg",@"yuan.jpg",@"yuan.jpg",@"yuan.jpg", nil]; self.imagelist = image;//创建元素为图片的数组 NSArray *detal = [NSArrayarrayWithObjects:@"详细信息", @"详细信息",@"详细信息",@"详细信息",nil]; self.detallist = detal;//创建元素为详细信息的数组 NSArray *color = [NSArrayarrayWithObjects:@"黄色",@"黑色",@"黄色",@"白色", nil]; self.colorlist = color;//创建元素为颜色的数组 UITableView *tableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];//创建一个tableview 大小和view的大小一样 style是plain self.myTableView = tableView; tableView.dataSource = self; tableView.delegate = self;//实现代理 [self.viewaddSubview:self.myTableView];//加载myTableView }//ViewController 需要在View上建立一个tableview,需要实现代理 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.textlistcount]; //Section的行数 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { staticNSString *CellWithIndentifier = @"Cell"; NSInteger row = indexPath.row; TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIndentifier];//cell的重用机制 if(cell == nil) { cell = [[TableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellWithIndentifier]; //UITableViewCellStyleSubtitle 的样式是有图片大标题和小标题 } //NSUInteger row = [indexPath row]; cell.lbText.text = [self.textlistobjectAtIndex:row] cell.lbDetail.text = [self.detallistobjectAtIndex:row]; cell.imageView.image = [UIImageimageNamed: [self.imagelistobjectAtIndex:row]]; cell.lbColor.text = [self.colorlistobjectAtIndex:row];//把蚊子和图片放到cell对应的位置 return cell; } - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
Tableviewcell.h #import <UIKit/UIKit.h> @interface TableViewCell (自己起的名字): UITableViewCell @property(nonatomic,retain)UILabel *lbText; @property(nonatomic,retain)UILabel *lbDetail; @property(nonatomic,retain)UIImageView *lbImage; @property(nonatomic,retain)UILabel *lbColor; //建立label来放需要的文字 建立imageview来显示图片 @end Tableviewcell.m #import "TableViewCell.h" @implementation TableViewCell - (void)awakeFromNib { // Initialization code } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ self.lbText = [[UILabelalloc]initWithFrame:CGRectMake(70, 30, 50, 50)]; //设置lbText的位置 [selfaddSubview:self.lbText];//加载lbText self.lbDetail = [[UILabelalloc]initWithFrame:CGRectMake(70, 50, 250, 50)]; [selfaddSubview:self.lbDetail];//加载lbDetail //设置lbDetail的位置 self.lbImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 45, 40,40)]; //设置lbImage的位置 [self addSubview:self.lbImage];//加载lbImage self.lbColor = [[UILabelalloc]initWithFrame:CGRectMake(300, 50, 100, 50)]; [selfaddSubview:self.lbColor];//加载lbColor //设置lbColor的位置 } returnself; }//用TableViewCell来控制label的位置 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [supersetSelected:selected animated:animated]; // Configure the view for the selected state } @end
结果如图