[英]Remove UILabels from UITableViewCell and add them again
我要從UITableViewCell
刪除標簽,因為在該特定單元格中不需要它們。 問題是當單元被重用時,我需要它們,但之前已將它們刪除了。
if (post.blockContent == TRUE) {
[cell.titleLabel removeFromSuperview];
[cell.contentLabel removeFromSuperview];
}
如何將它們再次添加到UITableViewCell
?
我刪除它們是因為我在約束所有內容與動態單元格高度之間存在約束,而我不能簡單地隱藏它們,因為這只會在單元格的中間留出空白。
喜歡
// set visibile for all cell
[cell.contentView addSubview:cell.titleLabel];
[cell.contentView addSubview:cell.contentLabel];
// when contindition statisfy it will be hide
if (post.blockContent == TRUE) {
[cell.titleLabel removeFromSuperview];
[cell.contentLabel removeFromSuperview];
}
選擇2
cell.titleLabel.hidden = NO;
cell.contentLabel.hidden = NO;
if (post.blockContent == TRUE) {
cell.titleLabel.hidden = YES;
cell.contentLabel.hidden = YES;
}
tableview重用了單元格。 因為這個原因,您不能使用removeFromSuperview,因為使用同一實例的所有單元都將刪除標簽。
解決方案是使用的約束。 您需要包裝標簽以查看,其他對象將具有跳過該視圖並減小單元格常數的約束。
在heightForRow中,您需要計算沒有視圖的高度。
在運行時更改優先級,這是解決方案的主要思想。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.