第一种 直接创建一个可变数组
_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值
-(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;
}