一、自定义UITableViewCell
eg.
CZItemCell.h
#import <UIKit/UIKit.h>
@class CZItem;
@interface CZItemCell : UITableViewCell
//创建一个可重用的自定义cell
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (nonatomic, strong) CZItem *item;
@end
CZItemCell.m
#import "CZItemCell.h"
#import "CZItem.h"
#import "CZItemArrow.h"
#import "CZItemSwitch.h"
@implementation CZItemCell
//1 创建一个可重用的自定义cell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *reuseId = @"item";
CZItemCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
}
return cell;
}
//2
- (void)setItem:(CZItem *)item{
_item = item;
self.textLabel.text = item.title;
if (item.icon) {
self.imageView.image = [UIImage imageNamed:item.icon];
}
//判断当前的模型是箭头还是开关
if ([item isKindOfClass:[CZItemArrow class]]) {
//设置箭头
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];
}else if([item isKindOfClass:[CZItemSwitch class]]){
//不允许cell选中
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.accessoryView = [UISwitch new];
}else{
self.accessoryView = nil;
}
}
@end