您的位置:首页 > 其它

自定义tableViewCell和设置多选

2015-09-01 18:20 337 查看
TableView.m 头文件

#import <UIKit/UIKit.h>

@interface SmallKindsTableVC : UITableViewController
-(void)setNavigatonControllerTitle:(NSString*)title;
@end

TableView.h 头文件

#import "SmallKindsTableVC.h"
#import "SmallKindTableViewCell.h"
#import "DataSource.h"

@interface SmallKindsTableVC ()
@property (strong, nonatomic) NSString* bigKindName;
@property (strong, nonatomic) NSArray* contents;
@property (strong, nonatomic) NSArray* details;
@end

@implementation SmallKindsTableVC

- (void)viewDidLoad
{
[super viewDidLoad];

//添加工具栏
UIWindow* window = [UIApplication sharedApplication].keyWindow;

UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(window.bounds)-55.0f, CGRectGetWidth(window.bounds), 55.0f)];
view.backgroundColor = [UIColor colorWithRed:214.0f/255.0f green:214.0f/255.0f blue:214.0f/255.0f alpha:1.0f];

UIButton* submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
submitBtn.backgroundColor = [UIColor colorWithRed:62.0f/255.0f green:204.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
submitBtn.frame = CGRectMake(45, 10, CGRectGetWidth(view.frame)-90, CGRectGetHeight(view.frame)-20);
//设置button内容时,必须要调用它的方法赋值.
    //  submitBtn.titleLabel.text=@"确定";  调它的属性是没起作用的。
[submitBtn setTitle:@"确定" forState:UIControlStateNormal];
submitBtn.titleLabel.textColor = [UIColor blackColor];
submitBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
[submitBtn addTarget:self action:@selector(submitBtnEvent:) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:submitBtn];
[window addSubview:view];

//设置tableview多选
self.tableView.allowsMultipleSelectionDuringEditing = YES;
[self.tableView setEditing:YES];//设置UITableView为编译状态
[self.tableView setSeparatorColor:[UIColor lightGrayColor]];//设置tableViewCell间的分割线的颜色

 //设置一个属性保存我当前选择的indexPath集合
NSArray* indexPaths=[self.tableView indexPathsForSelectedRows];

//读取数据
DataSource* dataSource = [DataSource shareDataSource];
self.contents = [[NSArray alloc]initWithArray:[dataSource getSmallKindContentsWithBigKind:self.bigKindName]];
self.details = [[NSArray alloc]initWithArray:[dataSource getSmallKindDetailsWithBigKind:self.bigKindName]];
}
-(void)setNavigatonControllerTitle:(NSString*)title
{
self.bigKindName = title;
self.navigationItem.title = title;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.contents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* identifier=[NSString stringWithFormat:@"%ld",indexPath.row];
SmallKindTableViewCell *cell  =  [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[SmallKindTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// 自定义UITableViewCell选中时背景
        UIView* view = [[UIView alloc]initWithFrame:cell.frame];
        view.backgroundColor=[UIColor whiteColor];
        cell.selectedBackgroundView = view;
 }
[cell setContentLabelText:self.contents[indexPath.row] detailLabelText:self.details[indexPath.row]];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0f;
}

自定义cell.m 文件

#import <UIKit/UIKit.h>

@interface SmallKindTableViewCell : UITableViewCell
-(void)setContentLabelText:(NSString *)contentText detailLabelText:(NSString *)detailText;
@end


自定义cell.h 文件

#import "SmallKindTableViewCell.h"

@implementation SmallKindTableViewCell
{
UILabel* contentLabel;
UILabel* detailLabel;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
//  [contentLabel sizeToFit];//自动设置label的高
//  初始化label
contentLabel = [[UILabel alloc]init];
contentLabel.textAlignment = NSTextAlignmentNatural;
contentLabel.text = @"contentText";//给初始文本
contentLabel.textColor = [UIColor blackColor];
contentLabel.font = [UIFont systemFontOfSize:16.0f];//设置字体
[contentLabel setNumberOfLines:1];//设置行数

detailLabel = [[UILabel alloc]init];
detailLabel.text = @"deatailText";
detailLabel.textColor = [UIColor lightGrayColor];
detailLabel.textAlignment = NSTextAlignmentNatural;
detailLabel.font = [UIFont systemFontOfSize:14.0f];
[detailLabel setNumberOfLines:1];

}
return self;
}
-(void)setContentLabelText:(NSString *)contentText detailLabelText:(NSString *)detailText
{
contentLabel.text = contentText;
detailLabel.text = detailText;

//设置label根据自身内容调整对应的宽度和高度,最开始是不能给label的frame的
//设置时,必须是在给定label内容时才设置添加的
//获取字符串的高度和宽度
CGSize contentLabelSize = [contentLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]}];
contentLabel.frame = CGRectMake(0, 0, contentLabelSize.width, CGRectGetHeight(self.contentView.frame));
[self.contentView addSubview:contentLabel];

CGSize detailLabelSize = [detailLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
detailLabel.frame=CGRectMake(CGRectGetWidth(contentLabel.frame), 0, detailLabelSize.width, CGRectGetHeight(self.contentView.frame));
[self.contentView addSubview:detailLabel];
}

@end


效果图如下:



另外注意的点:

1、dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别:

前者不必向tableView注册cell的Identifier,但需要判断获取的cell是否为nil;

后者则必须向table注册cell,可省略判断获取的cell是否为空,因为无可复用cell时runtime将使用注册时提供的资源去新建一个cell并返回

2、自定义cell时,记得将其他内容加到self.contentView 上,而不是直接添加到 cell 本身上

3、自定义cell时,若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib

不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:

4、使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: