import "BaseViewController.h"
#import "BaseViewController.h"
@interface BaseViewController ()
// 高度缓存
@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;
@end
@implementation BaseViewController
//主动实现代理 子类可不用实现 实现即为覆盖
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber *height = @(cell.frame.size.height);
[self.heightAtIndexPath setObject:height forKey:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
if(height)
{
return height.floatValue;
}
else
{
return 100;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
#pragma mark - Getters
- (NSMutableDictionary *)heightAtIndexPath
{
if (!_heightAtIndexPath) {
_heightAtIndexPath = [NSMutableDictionary dictionary];
}
return _heightAtIndexPath;
}
controller
#import "MyViewController.h"
#import "ListModel.h"
#import "MyTableViewCell.h"
@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIButton *moveBtn;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.tableView];
[self.view addSubview:self.moveBtn];
[self.view bringSubviewToFront:self.moveBtn];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyTableViewCell class]) forIndexPath:indexPath];
cell.model = self.dataArray[indexPath.row];
return cell;
}
#pragma mark - 懒加载
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.frame = CGRectMake(0, kTopHeight, kScreenWidth, 800);
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MyTableViewCell class])];
}
return _tableView;
}
- (UIButton *)moveBtn
{
if (!_moveBtn) {
_moveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_moveBtn.frame = CGRectMake(self.view.frame.size.width-100, 100, 100, 100);
_moveBtn.backgroundColor = [UIColor redColor];
_moveBtn.userInteractionEnabled = YES;
[_moveBtn addTarget:self action:@selector(goToAdvertDetail) forControlEvents:UIControlEventTouchUpInside];
}
return _moveBtn;
}
#pragma mark - Getters
- (NSMutableArray *)dataArray
{
if (!_dataArray)
{
_dataArray = [NSMutableArray array];
NSString *string = @"Siri 让你能够利用语音来完成发送信息、安排会议、查看最新比分等更多事务。只要说出你想做的事,Siri 就能帮你办到。Siri 可以听懂你说的话、知晓你的心意,甚至还能有所回应。iOS 7 中的 Siri 拥有新外观、新声音和新功能。它的界面经过重新设计,以淡入视图浮现于任意屏幕画面的最上层。Siri 回答问题的速度更快,还能查询更多信息源,如维基百科。它可以承担更多任务,如回电话、播放语音邮件、调节屏幕亮度,以及更多。";
//生成假数据
for (int i = 0; i < 100; i++)
{
ListModel *model = [[ListModel alloc] init];
NSInteger index = (arc4random()%(string.length / 20)) * 20;
model.desc = [string substringToIndex:MAX(20, index)];
[_dataArray addObject:model];
}
}
return _dataArray;
}
cell
#import "MyTableViewCell.h"
@interface MyTableViewCell()
@property(nonatomic,strong) UIImageView *headImg;
@property(nonatomic,strong) UILabel *titleLabel;
@end
@implementation MyTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (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) {
self.contentView.backgroundColor = [UIColor whiteColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setUpUI];
}
return self;
}
- (void)setModel:(ListModel *)model{
_model = model;
self.headImg.image = [UIImage imageNamed:@"timg"];
self.titleLabel.text = model.desc;
}
- (void)setUpUI{
[self.contentView addSubview:self.headImg];
[self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(self.contentView).offset(10);
make.width.mas_equalTo(120);
make.height.mas_equalTo(120);
}];
[self.contentView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.headImg);
make.left.equalTo(self.headImg.mas_right).offset(10);
make.height.mas_greaterThanOrEqualTo(self.headImg.mas_height); // 设置最小高度
make.bottom.right.equalTo(self.contentView).offset(-10);
}];
}
- (UIImageView *)headImg
{
if (!_headImg) {
_headImg = [[UIImageView alloc]init];
[self.contentView addSubview:_headImg];
}
return _headImg;
}
- (UILabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]init];
_titleLabel.numberOfLines = 0;
_titleLabel.font = [UIFont systemFontOfSize:15];
_titleLabel.textColor = [UIColor darkTextColor];
[self.contentView addSubview:_titleLabel];
}
return _titleLabel;
}
@end
网友评论