简体   繁体   中英

Change `UIView` Width as per `UILabel` Text in `UITableViewCell`

在我的UITableViewCell有一个带有Width Constraint的UIView ,在它里面有Image和UILabel

Add your view inside UIStackView and inside that view add your label and imageView and set proper constraint to label and imageView. and remove all code for calculation of size.

Use UIStackView . You don't have to think about anything.

在此处输入图片说明

在此处输入图片说明

Remove the arrAlreadyChangeCellFrames . A cell that was set once for one object can be reset for a different object so it has to be resized.

Tableviews and Collectionview work by reusing the cells. So that even if you scroll very fast there will only be a handful of cells in memory. Because of this there is a very important rule to always follow in cellForRowAtIndexPath : every if must have an else . Because the cell can be reused anything that you set in one case must be unset in the other case.

Also, for most layouts you should not need to set the width of label when the text changes. Just set the width to the maximum size that it is allowed to be and the text will fill up as much as it can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM