IOS -- 纯代码实现tableview
1.HMBookListenTimingView.h 添加
#import "NSObject+HMCategory.h"
<UITableViewDelegate,UITableViewDataSource>
self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, rect.size.height-60) style:UITableViewStylePlain]; self.tableview.scrollEnabled = NO; self.tableview.backgroundColor = [UIColor clearColor]; self.tableview.alpha = 0.9; self.tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine; self.tableview.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20); [_tableview registerClass:[HMBookListenTimingViewCell class] forCellReuseIdentifier:[HMBookListenTimingViewCell hm_className]]; _tableview.delegate = self; _tableview.dataSource = self; [self addSubview:_tableview];
协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { HMBookListenTimingViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[HMBookListenTimingViewCell hm_className] forIndexPath:indexPath]; _currentSelectTimingModel = _timeListenData[indexPath.row]; cell.leftLable.text = _currentSelectTimingModel.title; if (indexPath.row == self.timeListenData.count - 1) { [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } return cell; }
2.HMBookListenTimingViewCell.h
#import <UIKit/UIKit.h> #import "HMBookListenTimingManager.h" @interface HMBookListenTimingViewCell : UITableViewCell @property (nonatomic, strong) UILabel *leftLable; //左边标题 @property (nonatomic, strong) UILabel *countDownLable; //剩下的时间 @property (nonatomic, strong) UIImageView *chooseImageView; //选择图片 @end
HMBookListenTimingViewCell.m
#import "HMBookListenTimingViewCell.h" #import <Masonry.h> @implementation HMBookListenTimingViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor clearColor]; _leftLable = [[UILabel alloc] init]; _leftLable.textColor = [self colorWithHex:0x262626] ; _leftLable.font = [UIFont systemFontOfSize:15.0]; _leftLable.textAlignment = NSTextAlignmentLeft; [self.contentView addSubview:_leftLable]; [_leftLable mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView.mas_left).offset(30); make.top.equalTo(self.contentView.mas_top).offset(12); make.width.equalTo(@120); make.height.equalTo(@18); }]; _chooseImageView = [[UIImageView alloc] init]; _chooseImageView.image = [UIImage imageNamed:@"ic_listen_time_unselect.png"]; [self.contentView addSubview:_chooseImageView]; [_chooseImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView.mas_right).offset(-30); make.top.equalTo(self.contentView.mas_top).offset(11); make.width.equalTo(@22); make.height.equalTo(@22); }]; _countDownLable = [[UILabel alloc] init]; _countDownLable.textColor = [self colorWithHex:0x262626] ; _countDownLable.font = [UIFont systemFontOfSize:15.0]; _countDownLable.textAlignment = NSTextAlignmentRight; [self.contentView addSubview:_countDownLable]; [_countDownLable mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.chooseImageView.mas_left).offset(-15); make.top.equalTo(self.mas_top).offset(12); make.width.equalTo(@80); make.height.equalTo(@18); }]; } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (selected) { _chooseImageView.image = [UIImage imageNamed:@"ic_listen_time_select.png"]; } else { _chooseImageView.image = [UIImage imageNamed:@"ic_listen_time_unselect.png"]; } } //颜色处理 - (UIColor *)colorWithHex:(NSInteger)hexValue { return [self colorWithHex:hexValue alpha:1.0]; } - (UIColor *)colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue { return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16)) / 255.0 green:((float)((hexValue & 0xFF00) >> 8)) / 255.0 blue:((float)(hexValue & 0xFF)) / 255.0 alpha:alphaValue]; } @end