IOS自定义表格视图

来源:互联网 发布:mac抹除磁盘选什么格式 编辑:程序博客网 时间:2024/05/22 14:39


RecommendViewController.m文件

import"RecommendViewController.h"

#import"CustomTableView.h"


@interfaceRecommendViewController ()

{

    NSMutableArray * _dataSource;

    NSMutableArray * _data;

    NSMutableArray * _dataImages;

}

- (void)initData;

@end


@implementation RecommendViewController

- (void)dealloc

{

    [_dataSourcerelease];

    [_dataImagesrelease];

    [superdealloc];

}


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfinitData];

    

}



- (void)initData

{

    self.navigationItem.title =@"推荐";

    self.view.backgroundColor = [UIColorwhiteColor];

//从plist中读取数据

    _dataSource = [[NSMutableArrayalloc] initWithContentsOfFile:[[NSBundlemainBundle] pathForAuxiliaryExecutable:@"DetailInfomation.plist"]];

    

    _dataImages = [[NSMutableArrayalloc] 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 = [[CustomTableViewalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID];

        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    }

    cell.leftImageView.image = [UIImageimageNamed:_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

{

    [_leftImageViewrelease];

    [_topLablerelease];

    [_addressLabelrelease];

    [_phoneLabelrelease];

    [_stateLabelrelease];

    [superdealloc];

}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

       _leftImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(5,10, 110,90)];

       _leftImageView.layer.cornerRadius =7;

       _leftImageView.layer.masksToBounds =YES;

        [self.contentViewaddSubview:_leftImageView];

       _topLable = [[UILabelalloc] initWithFrame:CGRectMake(125,11, 120,25)];

       _topLable.font = [UIFontboldSystemFontOfSize:19];

       _topLable.textColor = [UIColororangeColor];

        [self.contentViewaddSubview:_topLable];

       _addressLabel = [[UILabelalloc] initWithFrame:CGRectMake(125,40, 140,21)];

       _addressLabel.font = [UIFontsystemFontOfSize:15];

       _addressLabel.textColor = [UIColorblackColor];

        [self.contentViewaddSubview:_addressLabel];

       _phoneLabel = [[UILabelalloc] initWithFrame:CGRectMake(125,70, 120,20)];

       _phoneLabel.font = [UIFontsystemFontOfSize:14];

       _phoneLabel.textColor = [UIColorgrayColor];

        [self.contentViewaddSubview:_phoneLabel];

       _stateLabel = [[UILabelalloc] initWithFrame:CGRectMake(270,16, 90,20)];

       _stateLabel.font = [UIFontboldSystemFontOfSize:18];

       _stateLabel.textColor = [UIColorredColor];

        [self.contentViewaddSubview:_stateLabel];

    }

    return self;

}


- (void)awakeFromNib {

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [supersetSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end


0 0
原创粉丝点击