iOS开发-自定义Cell以设置界面为例完成封装通用使用

//

//  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 = [[ZZBadgeButton alloc] init];

    }

    return _badgeButton;

}

 

- (UIImageView *)arrowView

{

    if (_arrowView == nil) {

        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_arrow"]];

    }

    return _arrowView;

}

 

- (UIImageView *)checkView

{

    if (_checkView == nil) {

        _checkView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_checkmark"]];

    }

    return _checkView;

}

 

- (UISwitch *)switchView

{

    if (_switchView == nil) {

        _switchView = [[UISwitch alloc] init];

    }

    return _switchView;

}

 

+ (instancetype)cellWithTableView:(UITableView *)tableView

{

    static NSString *ID = @"setting";

    ZZSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    

    if (cell == nil) {

        cell = [[ZZSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

        cell.tableView = tableView;

    }

    return cell;

}

 

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

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // 子标题

        UILabel *subtitleLabel = [[UILabel alloc] init];

        subtitleLabel.backgroundColor = [UIColor clearColor];

        subtitleLabel.textColor = [UIColor lightGrayColor];

        subtitleLabel.highlightedTextColor = subtitleLabel.textColor;

        subtitleLabel.font = [UIFont systemFontOfSize:12];

        [self.contentView addSubview:subtitleLabel];

        self.subtitleLabel = subtitleLabel;

        

        // 标题

        self.textLabel.backgroundColor = [UIColor clearColor];

        self.textLabel.textColor = [UIColor blackColor];

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

        self.textLabel.font = [UIFont boldSystemFontOfSize:15];

        

        // 最右边的详情文字

        self.detailTextLabel.backgroundColor = [UIColor clearColor];

        self.detailTextLabel.textColor = [UIColor lightGrayColor];

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

        self.detailTextLabel.font = [UIFont systemFontOfSize:13];

        

        self.backgroundColor = [UIColor clearColor];

    }

    return self;

}

 

- (void)setItem:(ZZSettingItem *)item

{

    _item = item;

    

    // 1.设置数据

    [self setupData];

    

    // 2.设置右边的控件

    [self setupRightView];

}

 

/**

 *  设置数据

 */

- (void)setupData

{

    // 1.图标

    if (self.item.icon) {

        self.imageView.image = [UIImage imageWithName: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

{

    [super layoutSubviews];

    

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

    

    // 设置子标题

    CGSize subtitleSize = [self.item.subtitle sizeWithFont: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;

    }

    [super setFrame:frame];

}

 

/**

 *  设置右边的控件

 */

- (void)setupRightView

{

    if (self.item.badgeValue)

    { // 右边显示数字

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

        self.accessoryView = self.badgeButton;

    } else if ([self.item isKindOfClass:[ZZSettingLabelItem class]]) { // 右边是文字

        self.accessoryView = nil;

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

        self.detailTextLabel.text = labelItem.text;

    } else if ([self.item isKindOfClass:[ZZSettingSwitchItem class]]) { // 右边是开关

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

        self.switchView.on = switchItem.isOn;

        self.accessoryView = self.switchView;

    } else if ([self.item isKindOfClass:[ZZSettingArrowItem class]]) { // 右边是箭头

        self.accessoryView = self.arrowView;

    } else if ([self.item isKindOfClass:[ZZSettingCheckItem class]]) { // 右边是打钩

        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.tableView numberOfRowsInSection: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 = [UIImage resizedImageWithName:bgName];

    self.selectedBg.image = [UIImage resizedImageWithName:selectedBgName];

}

 

@end

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值