【iOS】使用Xib自定义tableViewCell 获取数据

第一种 直接创建一个可变数组

_dataSource = [NSMutableArray arrayWithObjects:@"a",@"bdgdsgsdgsgaesfsdfsfsfs",@"c",@"d",@"a",@"b",@"c",@"d",@"e",@"f", nil];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId forIndexPath:indexPath];
    cell.hearImageView.image = [UIImage imageNamed:@"aio_icons_ptt"];
    cell.nameLabel.text = @"麻花头";
    cell.timeLabel.text = @"11.30 AM";
    cell.messageLabel.numberOfLines = 0;
    cell.messageLabel.text = _dataSource[indexPath.row];
    return cell;
}

以下三种都使用模型和懒加载

模型
#import <Foundation/Foundation.h>

@interface CLHero : NSObject
/** 姓名 */
@property (nonatomic,strong)  NSString *name;
/** 图片 */
@property (nonatomic,strong)  NSString *icon;
/** 小标题 */
@property (nonatomic,strong)  NSString *intro;

//-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)heroWithDict:(NSDictionary *)dict;

@end
  • 使用 KVC
#import "CLHero.h"

@implementation CLHero

+(instancetype)heroWithDict:(NSDictionary *)dict
{
    CLHero *hero = [[self alloc] init];
    //KVC 键值编码
    [hero setValuesForKeysWithDictionary:dict];
    return hero;
}
@end
懒加载
//懒加载
-(NSArray *)heroes
{
    if (_heroes == nil) {

        //加载 plist
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heroes.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *heroArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            CLHero *hero = [CLHero heroWithDict:dict];
            [heroArray addObject:hero];
        }
        _heroes = heroArray;
    }
    return _heroes;
}

第二种 懒加载 + tag 值

  • 设置控件的 tag值

    tag.png

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId forIndexPath:indexPath];
    //取出模型数据
    CLHero *hero = self.heroes[indexPath.row];
    UIImageView *hearImageView =(UIImageView *)[cell viewWithTag:20];
    hearImageView.image = [UIImage imageNamed:hero.icon];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:30];
    nameLabel.text = hero.name;
    UILabel *messageLabel = (UILabel *)[cell viewWithTag:40];
    messageLabel.text = hero.intro;

    NSLog(@"%p == %zd",cell,indexPath.row);

    return cell;

}

第三种 懒加载 + 属性

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId forIndexPath:indexPath];
    //取出模型数据
    CLHero *hero = self.heroes[indexPath.row];
    cell.hearImageView.image = [UIImage imageNamed:hero.icon];
    cell.nameLabel.text = hero.name;
    cell.messageLabel.text = hero.intro;
    cell.timeLabel.text = @"11:30 AM";
    return cell;
}

第四种 封装重写 + 懒加载

#import <UIKit/UIKit.h>
@class CLHero;
@interface CLTableViewCell : UITableViewCell

/** 模型 */
@property (nonatomic,strong)  CLHero *hero;

@end
  • 重写 hero 对象的set 方法
#import "CLTableViewCell.h"
#import "CLHero.h"

@interface CLTableViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *hearImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;

@end
@implementation CLTableViewCell

//把模型重写set 方法
-(void)setHero:(CLHero *)hero
{
    _hero = hero;

    //设置数据
//    UIImageView *iconView =(UIImageView *)[self viewWithTag:20];
    self.hearImageView.image = [UIImage imageNamed:hero.icon];
//    UILabel *titiLabel = (UILabel *)[self viewWithTag:30];
    self.nameLabel.text = hero.name;
//    UILabel *priceLabel = (UILabel *)[self viewWithTag:40];
    self. messageLabel.text = hero.intro;


}
  • 直接调用封装好的接口
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId forIndexPath:indexPath] ;
    cell.hero = self.heroes[indexPath.row];
    return cell;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值