看图片:
这是由混合Cell实现的,所以有两个Cell。里面分别是:
在Controller中代码如下:
#import "DiscoverViewController.h"
#import "DiscoverTableViewCell.h"
#import "Discover2TableViewCell.h"
@interface DiscoverViewController ()
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *groupArray;
@property(nonatomic, retain) NSMutableArray *imageArray;
@end
@implementation DiscoverViewController
- (void)dealloc{
[_tableView release], _tableView = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
[self.view addSubview:self.tableView];
}
- (UITableView *)tableView{
if (_tableView == nil) {
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.separatorColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1.0];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.groupArray = [NSMutableArray arrayWithObjects:@“”,@"朋友圈",@"",@"扫一扫",@"摇一摇",@"",@"附近的人",@"漂流瓶",@"",@"购物",@"游戏",@"", nil];
self.imageArray = [NSMutableArray arrayWithObjects:@"",@"ff_IconShowAlbum",@"",@"ff_IconQRCode",@"ff_IconShake",@"",@"ff_IconLocationService",@"ff_IconBottle",@"",@"CreditCard_ShoppingBag",@"MoreGame",@"", nil];
}
return _tableView;
}
#pragma mark----------------UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 12;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 5 || indexPath.row == 8 || indexPath.row == 11) {
static NSString *cellIntentifier = @"discoverCell";
DiscoverTableViewCell *discoverCell = [tableView dequeueReusableCellWithIdentifier:cellIntentifier];
if (discoverCell == nil) {
discoverCell = [[DiscoverTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIntentifier];
self.tableView.rowHeight = 30;
}
return discoverCell;
}
static NSString *cellIntentifier = @"discover2Cell";
Discover2TableViewCell *discover2Cell = [tableView dequeueReusableCellWithIdentifier:cellIntentifier];
if (discover2Cell == nil) {
discover2Cell = [[Discover2TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIntentifier];
self.tableView.rowHeight = 60;
}
discover2Cell.detailTextLabel.text = self.groupArray[indexPath.row];
discover2Cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",self.imageArray[indexPath.row]]];
return discover2Cell;
}
红线标记部分是图片和文字的实现,需要注意的地方是:划蓝线的部分。如果不写这些,会导致程序崩溃,因为数组越界。