0

I have some configuration based on the type of data I receive ( the insets to add in the cell.configure(with item: item) {}, however the cells are not re-used properly. I am using RxSwift for binding, though I don't see the issue in there.

tried setting image.layoutMargins = UIEdgeInsets.zero in prepareForReuse(), in the cell.configure(with: item) etc. Doesn't seem to work.

    brandsGroup
        .bind(to: rx.items(cellIdentifier: StudioCellConstants.brandReuseIdentifier,
                           cellType: BrandCollectionViewXibCell.self)) { (_, item, cell) in
                            cell.configure(with: item)
        }.disposed(by: disposeBag)

override func prepareForReuse() {
    super.prepareForReuse()
    image.layoutMargins = UIEdgeInsets.zero
}

Update:

Eventually I ended up setting back the frame like this, but it feels like a hacky solution. A better way would be much appreciated.

                image.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: imageContainerView.frame.width, height: imageContainerView.frame.height)
Adrian
  • 415
  • 4
  • 18
  • Elaborate this statement what does that mean cells are not re-used properly ?? what they are doing and what you want to achieve? tell your exacti issue and attach some screenShots of what you want and what you are currently getting? – Abu Ul Hassan Aug 05 '19 at 12:57
  • the UIEdgeInsets of the UIImageView are not reset when the model changes. ( read prepareForReuse() is called) setting UIEdgeInsets manually doesn't seem to work as well. I can't see why... – Adrian Aug 05 '19 at 13:05
  • before calling cell.configure(with: item) place following image.layoutMargins = UIEdgeInsets.zero – Abu Ul Hassan Aug 05 '19 at 14:09
  • or show us cell.configure method – Abu Ul Hassan Aug 05 '19 at 14:10
  • tried that, does not work. The cell configure method is an if/else statement which sets the insets based on a condition. I am using the instance method on the called withAlignmentRectInsets(_:) – Adrian Aug 06 '19 at 09:12
  • thats what i am saying set insets in both if and also in else condition too like if true { set margin }else { reset margin or margin for else condition. } reason. tableView resuses cell so instead of using new cell tableView uses one of old cell that have layout margin set, so it appears to be faulty – Abu Ul Hassan Aug 06 '19 at 09:14
  • 1
    if you show configure cell method i can elaborate that according to your scenario. – Abu Ul Hassan Aug 06 '19 at 09:18
  • nope, that's not the problem :) It is either something related to cell re-usage which I don't know about, some drawing thing related to UIKit. – Adrian Aug 06 '19 at 09:23
  • i have gone through if else condtion in cellForRow and cellForItem many times :) next its upto you, i alwasy found solution setting an alternative values for else conditions :) happy coding – Abu Ul Hassan Aug 06 '19 at 09:24
  • I eventually ended up setting the frame back like this: image.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: imageContainerView.frame.width, height: imageContainerView.frame.height)) but it feels like a hack. – Adrian Aug 06 '19 at 09:25
  • okay great, go with it until someday you will understand what the problem was and then replace it ;) – Abu Ul Hassan Aug 06 '19 at 09:27
  • also here is your answer https://stackoverflow.com/questions/27421469/setting-layoutmargins-of-uiview-doesnt-work Custom layoutMargin cannot work – Abu Ul Hassan Aug 06 '19 at 09:30

1 Answers1

0

Are you setting the content fill mode of imageview in cell correctly.

//Swift code(I am not familier in rxswift but the concept should be the same)

 let imageView = UIImageView()  
 imageView.contentMode = .scaleAspectFill

I prefered scaleAspectFill here as I assume u need the image to cover the whole imageView

Jeesson_7
  • 761
  • 1
  • 11
  • 38
  • thanks for the reply, though I don't see how is this related to my question. Setting the scale does not help. The content mode I have is image.contentMode = .scaleAspectFit. – Adrian Aug 05 '19 at 12:01
  • please check the question comments above – Adrian Aug 05 '19 at 13:25
  • There are many ways this could happen. but most possible outcome is the improper way of reusing cells..Check if your configuring the cell values each time a model is updated in cellForRow. Good luck – Jeesson_7 Aug 06 '19 at 09:37
  • yes, I checked that. The obvious answers didn't work, thus ended up on StackOverflow. – Adrian Aug 06 '19 at 09:44