動態計算tablviewcell高度
根據實體類的屬性,計算出要顯示的高度,然後設定tableview代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
THDiaryEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
return [THEntryCell heightForEntry:entry];
}
THEntryCell.m :
+ (CGFloat)heightForEntry:(THDiaryEntry *)entry {
const CGFloat topMargin = 35.0f;
const CGFloat bottomMargin = 80.0f;
const CGFloat minHeight = 106.0f;
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGRect boundingBox = [entry.body boundingRectWithSize:CGSizeMake(202, CGFLOAT_MAX) options:(NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName: font} context:nil];
return MAX(minHeight, CGRectGetHeight(boundingBox) + topMargin + bottomMargin);
}
注,cellForRowAtIndexPath方法裡最好不要太多的程式碼,把能抽取的程式碼都抽出來,比如設定屬性以及顯示在cell子類設定,看起來就清晰簡潔的多了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
THEntryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
THDiaryEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell configureCellForEntry:entry];
return cell;
}
THEntryCell.m:
- (void)configureCellForEntry:(THDiaryEntry *)entry {
self.bodyLabel.text = entry.body;
self.locationLabel.text = entry.location;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE, MMMM d yyyy"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:entry.date];
self.dateLabel.text = [dateFormatter stringFromDate:date];
if (entry.imageData) {
self.mainImageView.image = [UIImage imageWithData:entry.imageData];
} else {
self.mainImageView.image = [UIImage imageNamed:@"icn_noimage"];
}
if (entry.mood == THDiaryEntryMoodGood) {
self.moodImageView.image = [UIImage imageNamed:@"icn_happy"];
} else if (entry.mood == THDiaryEntryMoodAverage) {
self.moodImageView.image = [UIImage imageNamed:@"icn_average"];
} else if (entry.mood == THDiaryEntryMoodBad) {
self.moodImageView.image = [UIImage imageNamed:@"icn_bad"];
}
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame) / 2.0f;
if (entry.location.length > 0) {
self.locationLabel.text = entry.location;
} else {
self.locationLabel.text = @"No location";
}
}
相關文章
- 動態計算控制元件高度控制元件
- 動態生成DOM元素的高度及行數獲取與計算方法
- IOS 動態改變cell的高度iOS
- CSS 奇技淫巧:動態高度過渡動畫CSS動畫
- 【UniApp】-uni-app-動態計算字型大小(蘋果計算器)APP蘋果
- IM應用中如何計算富文字的高度
- css實現高度動態變化的佈局CSS
- ARM是計算領域重要動態的中心
- 實時計算 Flink> 產品簡介——最新動態
- 在動態IP下遠端連線計算機計算機
- 計算機網路實驗二:動態路由配置計算機網路路由
- iOS初級開發學習筆記:一個頁面中自動計算cell的高度來自適應tableView的高度iOS筆記View
- Android獲取狀態列高度Android
- web的高度計算-cell-裡面使用-只做記錄Web
- 大資料計算生態之資料計算(二)大資料
- 大資料計算生態之資料計算(一)大資料
- 表示式的動態解析和計算,Flee用起來真香
- 精確計算微信小程式scrollview高度,全機型適配微信小程式View
- 直播平臺搭建,動態設定ListView的高度的兩種方法View
- MySql資料儲存格式Compact及計算MySql的B+Tree高度MySql
- flink 有狀態(stateful)的計算
- 監聽div滾動高度
- 全球獨家視訊:特工隊 - 動態修改計算機系統呼叫計算機
- 計算機網路 : 靜態路由配置計算機網路路由
- 動態ip代理洞見:雲端計算、大資料、AI能稱雄大資料AI
- 動態程式設計(DynamicProgramming)程式設計
- 設計模式:動態代理設計模式
- 自動化所研發脈衝動態計算的毫瓦級超低功耗非同步感算一體類腦晶片非同步晶片
- 高度塌陷 解決高度塌陷 導航條 清除浮動 相對定位
- 扒一扒React計算狀態的原理React
- 樹2-二叉樹複製, 遍歷, 計算葉子結點和高度二叉樹
- 計算機啟動過程計算機
- CSS 也能實現 if 判斷?實現動態高度下的不同樣式展現CSS
- 高效能運算-粒子狀態模擬計算最佳化
- 靜態路由介紹(雲端計算隨筆)路由
- [效能優化]UITableView效能優化的一點感悟及計算UILabel高度的新方法優化UIView
- Java中計算移動平均線Java
- 如何實現高度自動化測試?
- iOS AutoLayout進階(五)UITableViewCell自動高度iOSUIView