RecommendViewController.m文件
import "RecommendViewController.h"
#import "CustomTableView.h"
@interface RecommendViewController ()
{
NSMutableArray * _dataSource;
NSMutableArray * _data;
NSMutableArray * _dataImages;
}
- (void)initData;
@end
@implementation RecommendViewController
- (void)dealloc
{
[_dataSource release];
[_dataImages release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initData];
}
- (void)initData
{
self.navigationItem.title = @"推荐";
self.view.backgroundColor = [UIColor whiteColor];
//从plist中读取数据
_dataSource = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"DetailInfomation.plist"]];
_dataImages = [[NSMutableArray alloc] initWithObjects:@"dog1.png", @"dog2.png",@"dog3.png",@"dog4.png",nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID = @"chongyong";
CustomTableView * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[CustomTableView alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.leftImageView.image = [UIImage imageNamed:_dataImages[indexPath.row]];
cell.topLable.text = _dataSource[indexPath.row][@"name"];
cell.addressLabel.text = _dataSource[indexPath.row][@"address"];
cell.phoneLabel.text = _dataSource[indexPath.row][@"phone"];
cell.stateLabel.text = _dataSource[indexPath.row][@"classify"];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{//单元格的组数
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//单元格的行数
return _dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ //单元格的高度
return 100;
}
@end
自定义单元格视图
CustomTableView.h文件
#import <UIKit/UIKit.h>
@interface CustomTableView : UITableViewCell
@property (nonatomic,retain)UIImageView * leftImageView;
@property (nonatomic,retain)UILabel * topLable;
@property (nonatomic,retain)UILabel * phoneLabel;
@property (nonatomic,retain)UILabel * addressLabel;
@property (nonatomic,retain)UILabel * stateLabel;
@end
CustomTableView.m文件
#import "CustomTableView.h"
@implementation CustomTableView
- (void)dealloc
{
[_leftImageView release];
[_topLable release];
[_addressLabel release];
[_phoneLabel release];
[_stateLabel release];
[super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 110, 90)];
_leftImageView.layer.cornerRadius = 7;
_leftImageView.layer.masksToBounds = YES;
[self.contentView addSubview:_leftImageView];
_topLable = [[UILabel alloc] initWithFrame:CGRectMake(125, 11, 120, 25)];
_topLable.font = [UIFont boldSystemFontOfSize:19];
_topLable.textColor = [UIColor orangeColor];
[self.contentView addSubview:_topLable];
_addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, 40, 140, 21)];
_addressLabel.font = [UIFont systemFontOfSize:15];
_addressLabel.textColor = [UIColor blackColor];
[self.contentView addSubview:_addressLabel];
_phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, 70, 120, 20)];
_phoneLabel.font = [UIFont systemFontOfSize:14];
_phoneLabel.textColor = [UIColor grayColor];
[self.contentView addSubview:_phoneLabel];
_stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 16, 90, 20)];
_stateLabel.font = [UIFont boldSystemFontOfSize:18];
_stateLabel.textColor = [UIColor redColor];
[self.contentView addSubview:_stateLabel];
}
return self;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end