之前,我们已经创建了一个简单的表视图App,用来显示菜单列表和图片。
下面,我们继续改进该App,是其效果更佳。
1)实现不同的行显示不同的图片
2) 定制表视图单元格
1.显示不同的缩略图:
在修改代码之前,我们先回顾一下在数据行上显示缩略图的代码:
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"simpleTableItem" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :simpleTableIdentifier];
if (cell == nil ) {
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :simpleTableIdentifier];
}
cell. textLabel . text = [ tableData objectAtIndex :indexPath. row ];
cell. imageView . image = [ UIImage imageNamed : @"creme_brelee.jpg" ];
return cell;
}
@implementation SimpleTableViewController
{
NSArray * tableData;
NSArray * thumbnails;
NSString * prepTime;
NSArray * time;
}
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//Initialize table data
tableData = [ NSArray arrayWithObjects : @"Egg Benedict" , @"Mushroom Risotto" , @"Full Breakfast" , @"Hamburger" , @"Ham and Egg sandwith" , @"Creme Brelee" , @"White Chcolate Dount" , @"Starbucks Coffee" , @"Vegetable Curry" , @"Instant Noodle with Egg" , @"Noodle with BBQ Pork" , @"Japanese Noodle with Pork" , @"Green Tea" , @"Thai Shrimp Cake" , @"Angry Birds Cake" , @"Ham and Cheese Panini" , nil ];
//Initialize thumbnails
thumbnails = [ NSArray arrayWithObjects : @"egg_benedict.jpg" , @"mushroom_risotto.jpg" , @"full_breakfast.jpg" , @"hamburger.jpg" , @"ham_and_egg_sandwich.jpg" , @"creme_brelee.jpg" , @"white_chocolate_donut.jpg" , @"starbucks_coffee.jpg" , @"vegetable_curry.jpg" , @"instant_noodle_with_egg.jpg" , @"noodle_with_bbq_pork.jpg" , @"japanese_noodle_with_pork.jpg" , @"green_tea.jpg" , @"thai_shrimp_cake.jpg" , @"angry_birds_cake.jpg" , @"ham_and_cheese_panini.jpg" , nil ];
//Initialize prepTime
prepTime = @"PrepTime:" ;
//Initialize time
time = [ NSArray arrayWithObjects : @"30 min" , @"20 min" , @"10 min" , @"40 min" , @"50 min" , @"70 min" , @"90 min" , @"30 min" , @"30 min" , @"30 min" , @"30 min" , @"30 min" , @"30 min" , @"30 min" , @"30 min" , @"30 min" , nil ];
}
@property ( nonatomic , weak ) IBOutlet UILabel *nameLabel;
@property ( nonatomic , weak ) IBOutlet UILabel *prepTimeLabel;
@property ( nonatomic , weak ) IBOutlet UIImageView *thumbnailImageView;
@property ( nonatomic , weak ) IBOutlet UILabel *time;
@synthesize nameLabel = _nameLabel;
@synthesize prepTimeLabel = _prepTimeLabel;
@synthesize thumbnailImageView = _thumbnailImageView;
@synthesize time = _time;
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell" ;
SimpleTableCell *cell = ( SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier :simpleTableIdentifier];
if (cell == nil ) {
NSArray *nib = [[ NSBundle mainBundle ] loadNibNamed : @"SimpleTableCell" owner : self options : nil ];
cell = [nib objectAtIndex : 0 ];
}
cell. nameLabel . text = [ tableData objectAtIndex :indexPath. row ];
cell. thumbnailImageView . image = [ UIImage imageNamed :[ thumbnails objectAtIndex :indexPath. row ]];
cell. prepTimeLabel . text = prepTime ;
cell. time . text = [ time objectAtIndex :indexPath. row ];
return cell;
}
然后,在更新好代码之后,Xcode检测到一些错误,并显示在源码编辑器中:
这是因为我们少引用SimpleTableCell.h 头文件。
最后,因为表单元格的高度更改为78,因此在@end代码行之前添加如下代码:
- ( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath
{
return 78 ;
}