0

i am trying to get similar effect of the UITableViewCell in iOS FaceBook App

so i subclassed a UITableViewCell and in the layoutSubviews

i use this code to resize the cell frame

self.frame = CGRectInset(self.frame, 1.5, 1.5)

and for the rounded rect of the cell

self.layer.cornerRadius = 10.0

and it's working with no problem but once any cell gets highlighted the cell gets resized every time the cell is highlighted i think it's because the layoutSubviews called more than once so it's applying the effect every time

Before highlight

After

is there anyway to fix this ?

Amr Mohamed
  • 2,290
  • 4
  • 20
  • 39
  • 1
    I would suggest you add a view for the background with constraints pinning it to the edges and set it to the background color you want. Then add a subview which has constraints pinning its edges to the superview with the margin you want. Put your content in this view. Create an IBOutlet for the inner view and in layoutSubviews set the corner radius for the inner views layer. The constraints should then do all the work for you to maintain the margin on all devices. – Rory McKinnel Jun 19 '15 at 12:58
  • i was using this approach while ago but i want to tryout this so it's like facebook `UITableView` when you select a cell it only select the cell with `subviews clipped` and also for multiple diff cell this would take much time building UI – Amr Mohamed Jun 19 '15 at 13:14
  • Not sure how layout contraints would change any clipping. All constraints really do is adjust the frame for you using rules rather than doing it all manually like you are trying to do. Regardless, you kind of answered your own question in the question: just have a flag property in the cell so you only do the inset change once. – Rory McKinnel Jun 19 '15 at 13:28
  • one flag won't work , dequeuing cell will need to call the layoutSubviews again and again so we need to have multiple flags to fix this even multiple flags will have a problem when going down and then up you will need to reset the flagged cells it's kinda annoying approach maybe or i am missing something ? – Amr Mohamed Jun 19 '15 at 13:42
  • 1
    You can detect when a cell will be reused by implementing `prepareForReuse` and reset your flag. You could also track the last frame size you set, and if it has changed next time you are in layoutSubviews, do your adjustment again as you can assume its been changed by something else. However I stick by my recommendation to do all this using constraints. Especially if you have variable subview content which also needs layed out. – Rory McKinnel Jun 19 '15 at 13:55
  • i think tracking the frame size is the best option i found and i am also going to stick with your first suggestion for now until something new come out or some one answer this question later , Thanks @RoryMcKinnel – Amr Mohamed Jun 19 '15 at 16:40
  • You can adjust layout constraints manually, as well. If you haven't explored this, you should. – AMayes Jun 19 '15 at 16:49

0 Answers0