美文网首页
TableView自定义一种cell

TableView自定义一种cell

作者: PZcoder | 来源:发表于2017-05-17 11:39 被阅读14次

TableView自定义一种cell,展示同一个数据实体的数据:

一、 ViewController中或TableViewController中基本设置:
(TableViewController中以下self.tableview改成self即可。)

1.遵循协议(数据源协议、代理协议)

<UITableViewDataSource,UITableViewDelegate>

2.指定委托(viewdidload中指定委托,基本这样写。。)

    self.tableView.dataSource = self;
    self.tableView.delegate = self;

获取数据、绑定cell

    //获取要展示的数据
    self.marr = [self.manager requestData];
    //绑定cell
    [self.tableView registerClass:[PersonCell class] forCellReuseIdentifier:NSStringFromClass([Person class])];

3.实现方法(数据源方法,委托方法)

//获取一共有几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//获取要展示的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.marr.count;
}

//获取每行要展示的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Person * per = self.marr[indexPath.row];
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Person class]) forIndexPath:indexPath];
    [cell setCells:per];
    return cell;
}

//改变每行的高度方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

二、数据实体设置

//设置需要展示的几个属性,可以赋值取值即可。
#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic,copy)NSString * name;
@property (nonatomic,copy)NSString * tel;
@property (nonatomic,copy)NSString * head;

@end

三、Cell的设置

1、.h中设置获取数据的方法

#import <UIKit/UIKit.h>
#import "Person.h"

@interface PersonCell : UITableViewCell

-(void)setCells:(Person *)person;

@end

2、.m中初始化要用的组件,实现获取数据的方法

#import "PersonCell.h"

@interface PersonCell ()
//设置要展示的组件
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UILabel * telLabel;
@property (nonatomic,strong)UIImageView * headImageView;

@end

@implementation PersonCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
    //要在此处初始化组件,可以使用自动布局(autolayout)。
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(49, 2, 150, 20)];
        self.nameLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.nameLabel.font = [UIFont systemFontOfSize:18];
        
        self.telLabel = [[UILabel alloc]initWithFrame:CGRectMake(49, 24, 150, 20)];
        self.telLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.telLabel.font = [UIFont systemFontOfSize:12];
        
        self.headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 40, 40)];
        self.headImageView.layer.cornerRadius = 10;
        self.headImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.telLabel];
        [self.contentView addSubview:self.headImageView];
    }
    
    return self;
}
-(void)setCells:(Person *)person
{
    //此处给组件赋值。
    self.nameLabel.text = person.name;
    self.telLabel.text = person.tel;
    self.headImageView.image = [UIImage imageNamed:person.head];
}

相关文章

网友评论

      本文标题:TableView自定义一种cell

      本文链接:https://www.haomeiwen.com/subject/gfkdxxtx.html