1.創(chuàng)建model
@interface Model : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, assign) CGFloat cellHeight;
#import "Model.h"
@implementation Model
- (void)setTitle:(NSString*)title
{
? ? _title= title;
? ?CGFloat w = [UIScreen mainScreen].bounds.size.width - 24;
? ? CGFloath =MAXFLOAT;
? ? NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
? ? CGFloat height = [title boundingRectWithSize:CGSizeMake(w, h) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
? ? _cellHeight= height +24;
}
2自定義cell
#import
#import "Model.h"
NS_ASSUME_NONNULL_BEGIN
@interfaceTableViewCell :UITableViewCell
@property (nonatomic, strong)Model *model;
@property? (nonatomic,copy)void(^moreButtonAction)(UIButton*button);
#import "TableViewCell.h"
#import "Masonry/Masonry.h"
@interface? TableViewCell()
@property (nonatomic, strong) UILabel *contentL;
@property (nonatomic, strong)UIButton? *moreButton;
@end
@implementation TableViewCell
- (void)awakeFromNib {
? ? [super awakeFromNib];
? ? // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
? ? if(self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
? ? ? ? self.clipsToBounds = YES;
? ? ? ? self.selectionStyle = UITableViewCellSelectionStyleNone;
? ? ? ? [selflayout];
? ? }
? ? return self;
}
- (void)layout
{
? ? [self.contentL mas_remakeConstraints:^(MASConstraintMaker *make) {
? ? ? ? make.bottom.mas_equalTo(-12);
? ? ? ? make.left.top.mas_equalTo(12);
? ? ? ? make.right.mas_equalTo(-12);
? ? }];
//? ? [self.moreButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//? ? ? ? make.bottom.mas_equalTo(-12);
//? ? ? ? make.right.mas_equalTo(-12);
//? ? }];
}
- (UIButton*)moreButton{
? ? if(_moreButton==nil) {
? ? ? ? _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
? ? ? ? [_moreButton setTitle:@"更多" forState:UIControlStateNormal];
? ? ? ? [_moreButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
? ? ? ? [_moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? [self.contentView? addSubview:_moreButton];
? ? }
? ? return _moreButton;
}
- (UILabel*)contentL
{
? ? if(!_contentL) {
? ? ? ? _contentL= [[UILabelalloc]init];
? ? ? ? _contentL.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, 0);
? ? ? ? _contentL.lineBreakMode = NSLineBreakByTruncatingTail;
? ? ? ? _contentL.numberOfLines = 4;
? ? ? ? [self.contentView addSubview:_contentL];
? ? }
? ? return _contentL;
}
- (void)setModel:(Model*)model{
? ? _model= model;
? ? self.contentL.text= model.title;
? ? if(model.isOpen) {
? ? ? ? self.contentL.numberOfLines=NSIntegerMax;
? ? }else{
? ? ? ? self.contentL.numberOfLines=4;
? ? }
}
- (void)moreButtonAction:(UIButton*)moreButton{
? ? if (self.moreButtonAction) {
? ? ? ? self.moreButtonAction(moreButton);
? ? }
}
3.viewConrtroller的內容
import "ViewController.h"
#import "Model.h"
#import "TableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *testDataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? [self.view addSubview:self.tableView];
? ? // Do any additional setup after loading the view, typically from a nib.
}
- (NSMutableArray*)testDataArray
{
? ? if (!_testDataArray) {
? ? ? ? NSArray *array = @[@"騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務院溺、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺火的。騰訊網(wǎng)下設新聞君仆、科技痪欲、財經(jīng)妻献、娛樂慌盯、體育掌挚、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務征唬、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺捌显。騰訊網(wǎng)下設新聞、科技总寒、財經(jīng)扶歪、娛樂、體育摄闸、汽車击罪、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺贪薪。騰訊網(wǎng)下設新聞、科技眠副、財經(jīng)画切、娛樂、體育囱怕、汽車霍弹、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺娃弓。騰訊網(wǎng)下設新聞典格、科技、財經(jīng)台丛、娛樂耍缴、體育、汽車挽霉、時尚.",
?? ? ? ? ? ? ? ? ? ? ? ? ? @"騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務防嗡、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞侠坎、科技蚁趁、財經(jīng)、娛樂实胸、體育他嫡、汽車番官、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺钢属。騰訊網(wǎng)下設新聞徘熔、科技、財經(jīng)署咽、娛樂近顷、體育、汽車宁否、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務窒升、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞慕匠、科技饱须、財經(jīng)、娛樂台谊、體育蓉媳、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務锅铅、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺酪呻。騰訊網(wǎng)下設新聞、科技盐须、財經(jīng)玩荠、娛樂、體育贼邓、騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務阶冈、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞塑径、科技女坑、財經(jīng)、娛樂统舀、體育匆骗、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務誉简、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺绰筛。騰訊網(wǎng)下設新聞、科技描融、財經(jīng)铝噩、娛樂、體育、汽車骏庸、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務毛甲、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞具被、科技玻募、財經(jīng)、娛樂一姿、體育七咧、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務叮叹、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺艾栋。騰訊網(wǎng)下設新聞、科技蛉顽、財經(jīng)蝗砾、娛樂、體育携冤、汽車",
?? ? ? ? ? ? ? ? ? ? ? ? ? @"騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務悼粮、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞曾棕、科技扣猫、財經(jīng)、娛樂翘地、體育苞笨、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務子眶、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞序芦、科技臭杰、財經(jīng)、娛樂谚中、體育渴杆、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直",
?? ? ? ? ? ? ? ? ? ? ? ? ? @"騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務宪塔、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺磁奖。騰訊網(wǎng)下設新聞、科技某筐、財經(jīng)比搭、娛樂、體育南誊、汽車身诺、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務蜜托、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。騰訊網(wǎng)下設新聞霉赡、科技橄务、財經(jīng)、娛樂穴亏、體育蜂挪、汽車、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務嗓化、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺棠涮。騰訊網(wǎng)下設新聞、科技蟆湖、財經(jīng)故爵、娛樂、體育隅津、汽車诬垂、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺伦仍。騰訊網(wǎng)下設新聞结窘、科技、財經(jīng)充蓝、娛樂隧枫、體育、汽車",
?? ? ? ? ? ? ? ? ? ? ? ? ? @"騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務谓苟、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺官脓。騰訊網(wǎng)下設新聞、科技涝焙、財經(jīng)卑笨、娛樂、體育仑撞、汽車赤兴、時尚.騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺隧哮。騰訊網(wǎng)下設新聞",
?? ? ? ? ? ? ? ? ? ? ? ? ? @"騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息,區(qū)域垂直生活服務桶良、社會化媒體資訊和產(chǎn)品為一體的互聯(lián)網(wǎng)媒體平臺。汽車",
? ? ? ? ? ? ? ? ? ? ? ? ? ];
? ? ? ?NSMutableArray *tempArray = [NSMutableArray array];
? ? ? ? for(NSString*strinarray) {
? ? ? ? ? ? Model*model = [Modelnew];
? ? ? ? ? ? model.title= str;
? ? ? ? ? ? model.isOpen=NO;
? ? ? ? ? ? [tempArrayaddObject:model];
? ? ? ? }
? ? ? ? _testDataArray= tempArray;
? ? }
? ? return _testDataArray;
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
? ? return self.testDataArray.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
? ? TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TableViewCell class])];
? ? Model*model =self.testDataArray[indexPath.row];
? ? cell.model= model;
//? ? cell.moreButtonAction = ^(UIButton * _Nonnull button) {
//? ? ? ? TableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//? ? ? ? [button setTitle:@"更多" forState:UIControlStateNormal];
//? ? ? ? cell.model.isOpen = !cell.model.isOpen;
//? ? ? ? [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//? ? };
? ? returncell;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
? ? TableViewCell*cell = [tableViewcellForRowAtIndexPath:indexPath];
? ? cell.model.isOpen = !cell.model.isOpen;
? ? [tableViewreloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath
{
? ? Model*model =self.testDataArray[indexPath.row];
? ? returnmodel.cellHeight;
}
#pragma mark--UITableViewDelegate,UITableViewDataSource
- (UITableView*)tableView{
? ? if(_tableView==nil) {
? ? ? ? _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
? ? ? ? _tableView.delegate=self;
? ? ? ? _tableView.dataSource=self;
? ? ? ? [_tableView? registerClass:[TableViewCell class] forCellReuseIdentifier:NSStringFromClass([TableViewCell class])];
? ? }
? ? return _tableView;
}