IOS自定义表格视图


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值