iOS之UITableView之样板代码

初次写博文,写的不好,呵呵,见谅......

做了很久的iOS了,大家都知道,在iOS里面用的最多的UI控键是什么?那就是UITableVIew,现在任何一个应用都不可能没有这个控键,但是大家没有觉得很这个控键的内部实现代码很繁琐么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    return cell;
}


每次实现UITableView里面的代码都很繁琐,特别是在高度自定义UITableViewCell的时候

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString  *CellIdentifier = @"cell";
    ZJHomeCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   
    if (cell == nil) {  
        cell = [[ZJHomeCustomCell alloc] initWithNbName:@"ZJHomeCustomCell"] ;
    }


不管大家觉不觉得,反正我觉得是,工作学习之余,自己写了一个关于UITableViewCell的smart样板代码。不废话,上代码

首先自定义一个类,继承UITableViewCell

ZJSmartTableVIewCell.h

@interface ZJSmartTableViewCell : UITableViewCell

+(id)cellForTableViewWithIdentifer:(UITableView*)tableView;
+(NSString*)identifier;
- (id)initWithIdentifier:(NSString*)cellID;
@end

ZJSmartTableVIewCell.m

#pragma mark 返回Cell对象,初始化都在里面完成
+(id)cellForTableViewWithIdentifer:(UITableView*)tableView;
{
    NSString * cellID = [self identifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[self alloc] initWithIdentifier:cellID];
    }
    return cell;
}
#pragma mark 返回当前类名字符串,也就是Cell重用卫衣标示符
+(NSString*)identifier
{
    return NSStringFromClass([self class]);
}
#pragma mark 这个方法是用户自定义方法,自己在这个方法里面自定义布局等等
- (id)initWithIdentifier:(NSString*)cellID
{
     return [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}


样板代码写好了,那么我们就用一用吧,首先我们创建一个类,继承 ZJSmartTabelViewCell

#import "ZJSmartTableViewCell.h"
@interface ZJCustom : ZJSmartTableViewCell

//自定义布局的成员变量
@property (nonatomic,retain) UILabel *customLabel;
@end

#import "ZJCustom.h"

@implementation ZJCustom

//如果需要自定义Cell,那么就重写这个方法吧
//- (id)initWithIdentifier:(NSString*)cellID
//{
//    self = [super initWithIdentifier:cellID];
//    if (self) {
//        _customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
//        _customLabel.backgroundColor = [UIColor orangeColor];
//        [self.contentView addSubview:_customLabel];
//    }
//    return self;
//}
@end



最后呢,既然 样板代码和自定义的Cell都写好了,那么我们就来用用吧

在UITableView的回调里面,我们只需要实例话ZJCustom就可以了,看见没有,一行代码搞定

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ZJCustom *cell = [ZJCustom cellForTableViewWithIdentifer:tableView];
    cell.textLabel.text = @"Test";
    return cell;
}


思路,就是把判断Cell的繁琐的代码抽出来到Cell的超类里面,自己来判断重用这块,这样抽取出来以后,那么,以后我们再就节省代码了。

这是代码书写的方法,如果用IB布局的话,方法也是一样的,这里就不写出来了,我想要是大家看了,也可以试一试自己封装一下.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值