(ios)一种好用的自定义Cell

作者: 三生石畔 | 来源:发表于2016-07-07 17:53 被阅读348次
    • 以后所有代码自定义cell,都可以 直接继承这个cell。 然后在
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //如下使用就可以了
          //ServerDetailHeaderCell *cell = [ServerDetailHeaderCell cellWithTableView:tableView];
                   // cell.model = self.modelDetail;
                 //   return cell;
    }
    

    中直接调用该Cell的初始化方法。就可以了。

    • 下面直接上代码,不BB了。
     //
    //  LTCell.h
    //  NSStringTest
    //
    //  Created by Apple on 16/3/24.
    //  Copyright © 2016年 wjx. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface LTCell : UITableViewCell
    /**
     *  1.Cell的复用
     *
     *  @param tableView <#tableView description#>
     *
     *  @return <#return value description#>
     */
    + (instancetype)cellWithTableView:(UITableView*)tableView;
    
    /**
     *  2.设置视图
     */
    - (void)setupUI;
    @end
    
    
    //
    //  LTCell.m
    //  NSStringTest
    //
    //  Created by Apple on 16/3/24.
    //  Copyright © 2016年 wjx. All rights reserved.
    //
    
    #import "LTCell.h"
    
    @implementation LTCell
    + (instancetype)cellWithTableView:(UITableView*)tableView
    {
        NSString *className = NSStringFromClass([self class]);
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:className];
        if (cell == nil) {
            cell = [[[super class] alloc]initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:className];
        }
        return (LTCell *)cell;
    }
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style
                  reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            [self setupUI];
        }
        return self;
    }
    
    - (void)setupUI
    {
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    //     分割线
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull subView, NSUInteger idx, BOOL * _Nonnull stop) {
    //        if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]) {
    //            subView.x = 0;
    //            subView.width = ScreenWidth;
    //            subView.height = 0.5;
    //            [subView setBackgroundColor:[STColor colorLine]];
    //        }
        }];
        
    }
    
    - (void)awakeFromNib {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    

    相关文章

      网友评论

        本文标题:(ios)一种好用的自定义Cell

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