简体   繁体   中英

iOS (Swift) - How to change any graphic (layout, size of UIView and etc.) in reusable UITableViewCell

The situation is pretty simple (as pretty common as I thought). I have an UITableView in my application that has lets say 3 rows (all are visible on the screen). Next I tap on one of them and this action changes something in all three cells (eg change scale of some UIView that is in this every cell) and everything is ok.

This is ok until I have only visible cells on the screen. I know that cellForRowAtIndexPath works only for visible cells. So how can we change such kind of properties (not data properties) for all the cells?

cellForRowAtIndexPath is where you prepare the cell for display. You should be doing the changes in here.

How are you modifying the view scale inside the cells when the user taps on a cell?

You should be triggering a reloadData() on the tableview which will reload the tableview and trigger the cellForRowAtIndexPath calls. You can also reload specific indexes inside the tableview if needed.

Inside your cellForRowAtIndexPath you should use some logic or state variable to control how you configure your cell. Dont dump all the code inside that function, pass arguments to your cell in a function eg configureCell(subviewScale: CGFloat) and let the cell configure itself.

Dont forget to reset the scale of the subview in prepareForReuse() on your custom cell so that when the cell is about to be dequeued and reused, it gets its properties reset.

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