iOS開發(fā)之帶你5分鐘封裝一個時間軸

時間軸在一些app中用的場景還不少,原理實現(xiàn)起來較為簡單,下面我們就來動手封裝一個比較常用的時間軸,具體效果看下圖:

Qinz

1.首先我們創(chuàng)建一個UIView,在上面放一個tableView,聲明一個方法,傳遞兩個參數(shù),第一個參數(shù)是需要將該時間軸放在哪個視圖上,第二個參數(shù)是傳遞數(shù)據(jù)源,頭文件下:

#import

@interfaceQinzTimeLine:UIView

@property(nonatomic,strong)NSArray*titleArr;

-(void)setSuperView:(UIView*)superView DataArr:(NSMutableArray*)dataArr;

@end

2.我們再來看看.m文件,也就是最簡單的tableView的應(yīng)用,這里有一個 [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class]?contentViewWidth:self.frame.size.width]方法是用到了SDAutoLayout這個庫用來自動計算cell高度的

#import"QinzTimeLine.h"

#import"SDAutoLayout.h"

#import"TimeLineCell.h"

@interfaceQinzTimeLine()

@property(nonatomic,strong)UITableView*tableView;

@property(nonatomic,strong)NSMutableArray*dataArr;

@end

@implementationQinzTimeLine

-(void)setSuperView:(UIView*)superView DataArr:(NSMutableArray*)dataArr{

self.frame = superView.bounds;

[superView addSubview:self];

[selfsetUp];

self.dataArr = dataArr;

}

-(void)setUp{

self.tableView = [[UITableViewalloc]init];

self.tableView.delegate =self;

self.tableView.dataSource =self;

self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;

[selfaddSubview:self.tableView];

self.tableView.sd_layout.topEqualToView(self).leftEqualToView(self).bottomEqualToView(self).rightEqualToView(self);

}

#pragma mark -- tableView的代理方法

#pragma mark -- 返回多少組

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

return1;

}

#pragma mark -- 每組返回多少個

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

returnself.dataArr.count;

}

#pragma mark -- 每個cell的高度

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

{

TimeLineModel*model =self.dataArr[indexPath.row];

return[self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model"cellClass:[TimeLineCellclass] contentViewWidth:self.frame.size.width];

}

#pragma mark -- 每個cell顯示的內(nèi)容

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

TimeLineCell*cell = [TimeLineCell timeLineCell:tableView];

if(indexPath.row ==0) {

cell.lineView.sd_layout.topSpaceToView(cell.pointView,0);

cell.lineView.backgroundColor = [UIColorgrayColor];

cell.pointView.backgroundColor = [UIColorredColor];

}else{

cell.lineView.sd_layout.topSpaceToView(cell.contentView,0);

cell.pointView.backgroundColor = [UIColorgrayColor];

cell.lineView.backgroundColor = [UIColorgrayColor];

}

cell.model =self.dataArr[indexPath.row];

cell.selectionStyle =UITableViewCellSelectionStyleNone;

returncell;

}

#pragma mark -- 選擇每個cell執(zhí)行的操作

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:NO];

}

3.關(guān)鍵在于tableViewCell布局,采用了Xib,方便對樣式進行設(shè)置,布局依然采用的是SDAutoLayout這個庫

圖片.png

4.看下布局代碼,這里對titleLB的布局做高度自適應(yīng),及設(shè)置autoHeightRatio為0即可,然后我們直接在設(shè)置模型中調(diào)用 ?[self setupAutoHeightWithBottomView:self.titleLB bottomMargin:0]就自動完成了高度自適應(yīng),是不是很方便

- (void)awakeFromNib {

[superawakeFromNib];

self.pointView.sd_layout.topSpaceToView(self.contentView,20).leftSpaceToView(self.contentView,5).widthIs(8).heightEqualToWidth();

self.pointView.sd_cornerRadius = @(4);

self.lineView.sd_layout.topEqualToView(self.contentView).centerXEqualToView(self.pointView).widthIs(1).bottomSpaceToView(self.contentView,0);

self.ttimeLB.sd_layout.centerYEqualToView(self.pointView).leftSpaceToView(self.pointView,10).rightSpaceToView(self.contentView,10).heightIs(20);

self.titleLB.sd_layout.topSpaceToView(self.ttimeLB,15).leftEqualToView(self.ttimeLB).rightSpaceToView(self.contentView,10).autoHeightRatio(0);

}

-(void)setModel:(TimeLineModel *)model{

_model = model;

self.titleLB.text= ?model.title;

self.ttimeLB.text = model.time;

[selfsetupAutoHeightWithBottomView:self.titleLB bottomMargin:0];

}

5.到此,封裝完畢,最后我們來看看控制器調(diào)用代碼

- (void)viewDidLoad {

[superviewDidLoad];

self.automaticallyAdjustsScrollViewInsets =YES;

self.timeLine = [[QinzTimeLine alloc]init];

[selfsetData];

[self.timeLine setSuperView:self.view DataArr:self.dataArr];

}

總結(jié):整體主要采用tableView進行布局,然后讓cell的高度自適應(yīng),需要注意的地方就是Cell中時間軸中的線條需要保持連貫,所以需要對第一個cell進行判斷,讓線條剛好與原點連接。

歡迎加入iOS技術(shù)交流群710170781,獲取更多有用技術(shù)干貨、文檔資料。有一句話叫三人行必有我?guī)?,歡迎進群共同進步。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容