0

I have a custom cell with a UIView that I am trying to update the frame on in cellForRowAtIndexPath. The frames are all updated successfully for the rows that load in the first pass of rows. However, the rows that need to be scrolled to be seen do not have their view frame updated until scrolling them into view, scrolling them out of view, and then scrolling them back into view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RoundCell *cell = (RoundCell *)[tableView dequeueReusableCellWithIdentifier:@"RoundCell"];

    CGRect frame = CGRectMake(160, 10, 50, 22);
    cell.rightConfidenceBar.frame = frame;
}

For the rows that are not initially loaded, why do I need to scroll them into view, out of view, then back into view before their UIView frame is updated?

Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • Why aren't you setting the frame of the `rightConfidenceBar` inside the implementation of the custom cell? That's where it belongs. – rmaddy Apr 25 '14 at 03:49
  • The real implementation is more complex and the frame (specifically width) of this bar needs to be set dynamically at runtime based on data received from a server. – Adam Johns Apr 25 '14 at 03:51
  • Have a look at the documentation about cell dequeing. https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier:forIndexPath: – khanh.tran.vinh Apr 25 '14 at 03:51
  • That doesn't change anything. Give the data to the cell and let the cell lay itself out. – rmaddy Apr 25 '14 at 03:51
  • I'm not sure what you mean. My custom cell only has a header file with properties connected to storyboard. – Adam Johns Apr 25 '14 at 03:53
  • @maddy I'm doing something like [this](http://www.appcoda.com/customize-table-view-cells-for-uitableview/). So I don't know where else I could be setting the size for this view. – Adam Johns Apr 25 '14 at 03:59
  • I do everything in code so my custom cells get their own .h and .m with all of the code needs to create, layout, and display their contents. I would image with Interface Builder you can set layout constraints on your view so it is positioned in the cell properly. BTW - my user id is `rmaddy`, not `maddy`. – rmaddy Apr 25 '14 at 04:08
  • just make sure that your cell's .xib view is not `auto Layout` if it is set as auto layout then it will not change frame. so make it `disable` for `auto Layout` – Haresh Ghatala Apr 25 '14 at 04:57
  • @rmaddy I should have been more clear earlier. Each row will have a different bar length based on an array of values. In reality I'm doing `cell.rightConfidenceBar.frame.size.width = myArray[indexPath.row]`, but my original post is a little simpler and is at the heart of the issue. – Adam Johns Apr 25 '14 at 15:07
  • You'd better adjust the layout in `- (void)layoutSubviews` of RoundCell. – KudoCC Apr 26 '14 at 03:55

2 Answers2

2

After doing some more digging and finding these questions:

question 1, question 2, question 3

I realized this was an issue with autolayout. The two solutions to this problem are to:

1.Turn off auto-layout by unchecking that box mentioned in one of those answers (not recommended)

2.Do what question 1 says and instead of updating the frame of the view, update the width constraint constant like so

for (NSLayoutConstraint *constraint in cell.rightConfidenceBar.constraints)
    {
        if (constraint.firstAttribute == NSLayoutAttributeWidth)
            constraint.constant = 50;
    }

BE WARNED: if you uncheck autolayout you may lose ALL your constraints you have made throughout your entire project.

Community
  • 1
  • 1
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • regarding 1, you can uncheck the autolayout only for that particular custom cell (select the cell and search for it in the file inspector) so there is no much drama. I did that and all worked just nice. – user1119517 May 13 '14 at 20:11
0

After

RoundCell *cell = (RoundCell *)[tableView dequeueReusableCellWithIdentifier:@"RoundCell"];

try adding

if (cell == nil)
    cell = [[RoundCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RoundCell"];
Jorn
  • 1,054
  • 9
  • 25