0

I have set up a horizontal UIScrollView in a UITableViewCell prototype in my storyboard, along with its content views. I have made use of Autolayout.

In the -cellForRowAtIndexPath: method, I am setting the contentSize as well as contentOffset for this scrollView.

The problem arises when the cells are created for the first time. Neither does the scrollView scroll, nor is the contentOffset taking place.

However, when I scroll down a few cells (when they start getting reused), the contentOffset takes place, and the scrollView is interactive.

Since I have subclassed the UITableViewCell, I have tried setting the contentOffset in the -awakeFromNib method, to no affect. I also tried doing so in the -tableView:willDisplayCell: method to no avail.

What could be going wrong? Is there a workaround?

There doesn't seem to be any relevant code that I could include. Please comment if you need more info.

EDIT I do not know whether this is relevant, but the Storyboard is showing a warning: Scroll View: Has ambiguous scrollable content width

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • did you try [cell setNeedsLayout] right after creating it within cellForRowAtIndexPath method? sometimes it's helpful – heximal Mar 20 '15 at 13:07
  • @ZeMoon, We have fetch same issue with Text view. In your case may be issue with Autolayout or cell Reusability. Please remove Autolayout then after remove cell reusability with Table view. Please Tried that and let us know your comments. – Renish Dadhaniya Mar 20 '15 at 13:13
  • @heximal You were on the right track. `[cell setNeedsLayout]` did not work, but `[cell layoutIfNeeded]` worked. – ZeMoon Mar 20 '15 at 13:17

1 Answers1

1

The issue was because the scrollView was not being laid out after setting the contentSize. This would not have been a problem if the contentSize had been set properly in the storyBoard itself (still dont know how to do that).

Anyways, in the cellForRowAtIndexPath method, calling

[cell layoutIfNeeded];

immediately after setting the contentSize worked.

A good explanation on how this method works can be found in this thread.

Community
  • 1
  • 1
ZeMoon
  • 20,054
  • 5
  • 57
  • 98