xcode 4.3.2 use storyboard 使用TableView,显示一个颜色列表,表格包括一张图片,一个文本,一行副文本。
TableView 呈现列表格式的数据,每一行是一个UITableViewCell对象,每个UITableViewCell可以显示文本标签(为textLabel),字幕(detailedTextLabel)和图像(ImageView)。
每一个TableView,需要有与之相关的委托(delegate)和一个数据源(DataSource)。
委托实现UITableViewDelegate的协议,并提供额外的控制的外观和功能表视图,包括检测用户触摸时特定的行,自定义行高和缩进,并实施行删除和编辑功能。
数据源,实现UITableViewDataSource协议,基本上包含方法定义标题信息,要显示多少行数据,如何将数据划分成不同的部分。
1,新建Single View Application ,use storyboard。
2,storyboard中ViewController中,拖入一个Table View ,设置TableView 的Outlets的dataSource和delegate 连线到View Controller
3,ViewController.h
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{ NSArray *colorName;// 数据源 } @property (nonatomic,retain) NSArray *colorName;
4,ViewController.m
@synthesize colorName; - (void)viewDidLoad { [super viewDidLoad]; // 初始化加载视图时的一些数据 colorName = [[NSArray alloc] initWithObjects:@"Red",@"Green",@"Yellow",@"Blue",@"White",@"Black", nil]; } /** 获取TableView显示的行数 */ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [colorName count]; } /** 定义Cell外观 */ -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } //设置图片,PolyStar.png是项目资源中的图片名 UIImage *image = [UIImage imageNamed:@"PolyStar.png"]; cell.imageView.image = image; //设置文字 NSString *color = [colorName objectAtIndex:[indexPath row]]; cell.textLabel.text = color; //设置详细介绍 NSString *desc = [NSString stringWithFormat:@"关于颜色"]; desc = [desc stringByAppendingFormat:color]; cell.detailTextLabel.text = desc; return cell; }
OK.
学习ing ...