這里主要是將上期的核心思路應(yīng)用到應(yīng)用到UITableView上來(lái)栗竖。
這里與上一期最大的不同就是在于,以cell來(lái)作為view的平臺(tái)。這里還有添加一些優(yōu)化性能的思路晦攒。
自定義cell(作為平臺(tái))
這里有個(gè)比較特殊的地方,也是它的優(yōu)勢(shì)的地方得哆,復(fù)用機(jī)制脯颜。所以在cell使用的控件上必須要在創(chuàng)建cell的同時(shí)創(chuàng)建其它的控件。
代碼:
+ (instancetype)addFrameworkCellTableViewCell:(UITableView *)tableView {
static NSString *ID = @"TYFrameworkCellTableViewCell";
TYFrameworkCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[TYFrameworkCellTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
[cell initView];
}
return cell;
}
- (void)initView {
TYFrameworkCellLayoutView *layoutView = [TYFrameworkCellLayoutView addFrameworkCellLayoutView];
[self addSubview:_layoutView = layoutView];
}
TYFrameworkCellLayoutView就是我們創(chuàng)建所有控件的類贩据,在addFrameworkCellLayoutView方法中我們只是做創(chuàng)建栋操,沒有涉及到布局。
獲取數(shù)據(jù)轉(zhuǎn)模型和簡(jiǎn)單處理布局?jǐn)?shù)據(jù)
在這里我們使用的是本地的json數(shù)據(jù)乐设,在獲取數(shù)據(jù)后通過創(chuàng)建子線程來(lái)做布局?jǐn)?shù)據(jù)計(jì)算讼庇。
+ (void)addWithFrameworkModelLayoutStr:(NSString *)str frameworkModel:(frameworkModelLayoutBlock)frameworkModel {
NSMutableArray *muArr = [NSMutableArray array];
NSArray *arr = [TYJsonData addJsonDataStr:str];
for (int i = 0; i < arr.count; i++) {
NSDictionary *dic = arr[i];
NSString *contentStr = dic[@"lblContent"];
dispatch_queue_t queue = dispatch_queue_create("frameworkModelLayout", DISPATCH_QUEUE_CONCURRENT);
//異步并發(fā)執(zhí)行
dispatch_async(queue, ^{
CGSize size = [TYJsonData addWithCalculateHeightStr:contentStr];
dispatch_sync(dispatch_get_main_queue(), ^{
NSString *heightStr = [NSString stringWithFormat:@"%f",size.height];
NSString *viewHeight = [NSString stringWithFormat:@"%f",400 + size.height + 50 + 20];
NSMutableDictionary *mutbleDic = [NSMutableDictionary dictionaryWithDictionary:dic];
[mutbleDic setValue:viewHeight forKey:@"viewHeight"];
[mutbleDic setValue:heightStr forKey:@"lblHeight"];
TYFrameworkCellModel *models = [TYFrameworkCellModel addWithModelDic:mutbleDic];
[muArr addObject:models];
if (i == arr.count - 1) {
if (models) {
frameworkModel(muArr);
}
}
});
});
}
}
這也算個(gè)性能優(yōu)化的點(diǎn),我們提前對(duì)布局進(jìn)行運(yùn)算近尚,不在主線程中進(jìn)行炒作蠕啄。
進(jìn)行頁(yè)面布局
- (void)introductionWithData:(TYFrameworkCellModel *)model sliding:(BOOL)sliding {
_layoutView.left = 0;
_layoutView.top = 0;
_layoutView.width = w;
_layoutView.height = [model.viewHeight floatValue];
[_layoutView addWithLayoutModel:model];
[TYFrameworkLogic addWithImageUrl:model sliding:sliding operationImage:^(NSString *urlStr) {
[_layoutView addWithImageUrl:urlStr];
}];
}
具體來(lái)說,這里我們進(jìn)行了兩步操作。布局和處理頁(yè)面邏輯數(shù)據(jù)歼跟,TYFrameworkLogic就是我們頁(yè)面數(shù)據(jù)邏輯處理和媳。
在這里主要處理的兩個(gè)性能問題,一個(gè)是就布局哈街,一個(gè)就是圖片的加載實(shí)際的處理留瞳。布局就是通過子線程來(lái)做運(yùn)算。圖片加載時(shí)機(jī)骚秦,這里就要通過UIScrollView來(lái)做處理了她倘。大致的思路就是,滑動(dòng)時(shí)不讓其進(jìn)行圖片加載作箍,當(dāng)滑動(dòng)停止時(shí)才對(duì)圖片進(jìn)行加載硬梁。
為什么要這樣做了,大家都知道異步加載圖片不影響主線程胞得,但圖片顯示出來(lái)就要在主線程上進(jìn)行操作荧止。因?yàn)閳D片是一張一張的請(qǐng)求,請(qǐng)求成功后直接加載到頁(yè)面上阶剑,這一過程就使用到了主線程跃巡。加載的多就會(huì)出現(xiàn)卡頓現(xiàn)象。