(ios)一種好用的自定義Cell

  • 以后所有代碼自定義cell,都可以 直接繼承這個cell。 然后在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//如下使用就可以了
      //ServerDetailHeaderCell *cell = [ServerDetailHeaderCell cellWithTableView:tableView];
               // cell.model = self.modelDetail;
             //   return cell;
}

中直接調(diào)用該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的復(fù)用
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
+ (instancetype)cellWithTableView:(UITableView*)tableView;

/**
 *  2.設(shè)置視圖
 */
- (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
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容