自定义Cell(以设置界面为例完成封装)(自定义Cell3⃣️)

来源:互联网 发布:软件开发合同 性质 编辑:程序博客网 时间:2024/06/05 10:18

//

//  ZZSettingCell.h

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/9.

//  Copyright (c) 2015 ZZ_Macpro. All rights reserved.

//


#import "ZZBgCell.h"


@class ZZSettingItem;


@interface ZZSettingCell :ZZBgCell


@property (nonatomic,strong) ZZSettingItem *item;

@property (nonatomic,strong) NSIndexPath *indexPath;


+ (instancetype)cellWithTableView:(UITableView *)tableView;


@end


//

//  ZZSettingCell.m

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/9.

//  Copyright (c) 2015 ZZ_Macpro. All rights reserved.

//


#import "ZZSettingCell.h"

#import "ZZSettingItem.h"

#import "ZZSettingArrowItem.h"

#import "ZZSettingSwitchItem.h"

#import "ZZSettingCheckItem.h"

#import "ZZSettingLabelItem.h"

#import "ZZBadgeButton.h"


@interface ZZSettingCell()


@property (weak, nonatomic) UITableView *tableView;

/**

 *  子标题

 */

@property (weak,nonatomic) UILabel *subtitleLabel;

/**

 *  箭头

 */

@property (strong,nonatomic) UIImageView *arrowView;

/**

 *  打钩

 */

@property (strong,nonatomic) UIImageView *checkView;

/**

 *  开关

 */

@property (strong,nonatomic) UISwitch *switchView;

/**

 *  提醒数字

 */

@property (strong,nonatomic) ZZBadgeButton *badgeButton;


@end


@implementation ZZSettingCell


- (ZZBadgeButton *)badgeButton

{

   if (_badgeButton ==nil) {

        _badgeButton = [[ZZBadgeButtonalloc] init];

    }

    return_badgeButton;

}


- (UIImageView *)arrowView

{

   if (_arrowView ==nil) {

        _arrowView = [[UIImageViewalloc] initWithImage:[UIImageimageWithName:@"common_icon_arrow"]];

    }

    return_arrowView;

}


- (UIImageView *)checkView

{

   if (_checkView ==nil) {

        _checkView = [[UIImageViewalloc] initWithImage:[UIImageimageWithName:@"common_icon_checkmark"]];

    }

    return_checkView;

}


- (UISwitch *)switchView

{

   if (_switchView ==nil) {

        _switchView = [[UISwitchalloc] init];

    }

    return_switchView;

}


+ (instancetype)cellWithTableView:(UITableView *)tableView

{

   static NSString *ID =@"setting";

    ZZSettingCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

    

   if (cell == nil) {

        cell = [[ZZSettingCellalloc] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:ID];

        cell.tableView = tableView;

    }

   return cell;

}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

   self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

   if (self) {

       // 子标题

       UILabel *subtitleLabel = [[UILabelalloc] init];

        subtitleLabel.backgroundColor = [UIColorclearColor];

        subtitleLabel.textColor = [UIColorlightGrayColor];

        subtitleLabel.highlightedTextColor = subtitleLabel.textColor;

        subtitleLabel.font = [UIFontsystemFontOfSize:12];

        [self.contentViewaddSubview:subtitleLabel];

       self.subtitleLabel = subtitleLabel;

        

       // 标题

        self.textLabel.backgroundColor = [UIColorclearColor];

        self.textLabel.textColor = [UIColorblackColor];

        self.textLabel.highlightedTextColor =self.textLabel.textColor;

        self.textLabel.font = [UIFontboldSystemFontOfSize:15];

        

        // 最右边的详情文字

        self.detailTextLabel.backgroundColor = [UIColorclearColor];

        self.detailTextLabel.textColor = [UIColorlightGrayColor];

        self.detailTextLabel.highlightedTextColor =self.detailTextLabel.textColor;

        self.detailTextLabel.font = [UIFontsystemFontOfSize:13];

        

        self.backgroundColor = [UIColorclearColor];

    }

    return self;

}


- (void)setItem:(ZZSettingItem *)item

{

   _item = item;

    

    // 1.设置数据

    [selfsetupData];

    

    // 2.设置右边的控件

    [selfsetupRightView];

}


/**

 *  设置数据

 */

- (void)setupData

{

    // 1.图标

   if (self.item.icon) {

        self.imageView.image = [UIImageimageWithName:self.item.icon];

    }

    

    // 2.标题

    self.textLabel.text =self.item.title;

    

    // 3.子标题

    if (self.item.subtitle) {

        self.subtitleLabel.hidden =NO;

        self.subtitleLabel.text =self.item.subtitle;

    }else {

        self.subtitleLabel.hidden =YES;

    }

}


- (void)layoutSubviews

{

    [superlayoutSubviews];

    

   if (self.item.subtitle ==nil) return;

    

    // 设置子标题

    CGSize subtitleSize = [self.item.subtitlesizeWithFont:self.subtitleLabel.font];

   CGFloat subtitleW = subtitleSize.width;

   CGFloat subtitleH = subtitleSize.height;

    CGFloat subtitleX =CGRectGetMaxX(self.textLabel.frame) +5;

   CGFloat subtitleY = (self.contentView.frame.size.height - subtitleH) * 0.5;

   self.subtitleLabel.frame =CGRectMake(subtitleX, subtitleY, subtitleW, subtitleH);

}


- (void)setFrame:(CGRect)frame

{

   if (iOS7) {

        frame.origin.x =ZZTableBorderW;

        frame.size.width -=2 * ZZTableBorderW;

    }else {

       CGFloat x = ZZTableBorderW -9;

        frame.origin.x = x;

        frame.size.width -=2 * x;

    }

    [supersetFrame:frame];

}


/**

 *  设置右边的控件

 */

- (void)setupRightView

{

    if (self.item.badgeValue)

    { // 右边显示数字

        self.badgeButton.value =self.item.badgeValue;

        self.accessoryView =self.badgeButton;

    } elseif ([self.itemisKindOfClass:[ZZSettingLabelItemclass]]) { //右边是文字

       self.accessoryView =nil;

        ZZSettingLabelItem *labelItem = (ZZSettingLabelItem *)self.item;

       self.detailTextLabel.text = labelItem.text;

    } elseif ([self.itemisKindOfClass:[ZZSettingSwitchItemclass]]) { //右边是开关

        ZZSettingSwitchItem *switchItem = (ZZSettingSwitchItem *)self.item;

       self.switchView.on = switchItem.isOn;

        self.accessoryView =self.switchView;

    } elseif ([self.itemisKindOfClass:[ZZSettingArrowItemclass]]) { //右边是箭头

        self.accessoryView =self.arrowView;

    } elseif ([self.itemisKindOfClass:[ZZSettingCheckItemclass]]) { //右边是打钩

        ZZSettingCheckItem *checkItem = (ZZSettingCheckItem *)self.item;

        self.accessoryView = checkItem.isChecked ?self.checkView :nil;

    }else { // 右边没有东西

       self.accessoryView =nil;

    }

}


- (void)setIndexPath:(NSIndexPath *)indexPath

{

   _indexPath = indexPath;

    

    int totalRows = [self.tableViewnumberOfRowsInSection:indexPath.section];

   NSString *bgName = nil;

   NSString *selectedBgName = nil;

   if (totalRows == 1) {// 这组就1

        bgName = @"common_card_background";

        selectedBgName = @"common_card_background_highlighted";

    }else if (indexPath.row ==0) { // 首行

        bgName = @"common_card_top_background";

        selectedBgName = @"common_card_top_background_highlighted";

    }else if (indexPath.row == totalRows -1) { // 尾行

        bgName = @"common_card_bottom_background";

        selectedBgName = @"common_card_bottom_background_highlighted";

    }else { // 中行

        bgName = @"common_card_middle_background";

        selectedBgName = @"common_card_middle_background_highlighted";

    }

    self.bg.image = [UIImageresizedImageWithName:bgName];

    self.selectedBg.image = [UIImageresizedImageWithName:selectedBgName];

}


@end



1 0