3

I am using UITableViewAutomaticDimension for calculating height of rows. Everything is working perfectly. Let say there are 100 rows in tableview The only issue i am facing is that when i call scrollToRowAtIndexPath, it loads all 100 cells again.

I am using dequeueReusableCellWithIdentifier but it seems when scrollToRowAtIndexPath, it loads all 100 cells again.

Here is my code:

In ViewDidLoad:

self.myTableView.estimatedRowHeight = EST_HEIGHT_CELL;
self.myTableView.rowHeight = UITableViewAutomaticDimension;

The delegate methods

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0)   {
    return EST_HEIGHT_CELL;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString * resuseID = @“MyCell”;
    MyCell * cell = [tableView dequeueReusableCellWithIdentifier:resuseID];
    if(!cell)   {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.cellIndex = indexPath;
    cell.myDelegate = self;
    return cell;
}

But when i call scrollToRowAtIndexPath, it loads all cells and the tableview HANGS...

dispatch_async(dispatch_get_main_queue(), ^{
    [self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:myIndex inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});

I have tried with animated:NO but still no success

If i give static height to heightForRowAtIndexPath, scrollToRowAtIndexPath doesn't load all the cells and tableview does not hangs, which is normal and acceptable.

What can be the issue? Thanks in advance.

Fast Coderz
  • 321
  • 1
  • 13
  • If you scroll to an `indexPath`, the `scrollView` **visually** scrolls through your cells, and therefore, your `cellForRowAtIndexPath` **will get called** for all the cells that it needs to load while scrolling. The reason your code is hanging is probably your methods in your `cellForRowAtIndexPath`, which looks like bad practice. Instead, create a class for your cell and register a nib for it in viewDidLoad. Check the highest voted answer in this question: http://stackoverflow.com/questions/14520495/uitableview-load-a-custom-cell-from-nib-file-throws-nsinternalinconsistencyexcep –  Mar 30 '17 at 20:46
  • I agree that tableView visually scrolls through the cell but it only loads those cells which are displayed on the screen. It doesn't make any sense to load all the cells when using Animated or Non Animated. If i return static value in heightForRowAtIndexPath `return 350.0` It works perfect and does not hangs The only issue is caused when using automatic dimension – Fast Coderz Mar 30 '17 at 21:02
  • FYI: There is no issue in loading the nib in `cellForRowAtIndexPath` because it is being reused using `dequeueReusableCellWithIdentifier` – Fast Coderz Mar 30 '17 at 21:08
  • If you read the documentation it says "Scrolls through the table view until a row identified by index path is at a particular location on the screen." That means, it will call the cellForRowAtIndexPath , otherwise it wouldnt have anything to scroll to from the first place. It would be something different if the method name would be someting like "startCellAtIndexPath", but this is not the case. For the automatic dimension , it is impossible to know where it freezes unless you run instruments. In my **opinion**, autodimension is useless or "not there yet" for most cases. –  Mar 30 '17 at 22:55
  • I didn't say it should be an issue, but bad practice since you have much cleaner way to register a nib as I mentioned. The cellForRowAtIndexPath method should be **minimal** and you should not do any heavy loading there, that in itself messes up your performance. BTW I noticed you don't have dequeueReusableCellWithIdentifier **forIndexPath**, check this thread http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi and let me know if that solves your problem, I think it should more or less. –  Mar 30 '17 at 22:57
  • @Sneak, i tried your solution, but that didn't helped :( – Fast Coderz Mar 31 '17 at 05:21
  • I think there might be some layout issues in your NIB or some other methods that gets called in your cell. As a last step, I would try to "clean it" out to see if there is any issues in the layout there, because it has to calculate all the layout for you, or something else, I would start with an empty nib and no functions, and add the elements to the view continuously to figure out where the "hang" problems begin. –  Mar 31 '17 at 12:15
  • 1
    @Sneak thanks for your suggestions and help, i have moved the automatic height calculation to static calculation and it is working perfectly. – Fast Coderz Mar 31 '17 at 12:19
  • NP, I would go that path too, I never use that automatic calculation, it may have its place for some UI , but in my experience it is not good if you want performance. :) –  Mar 31 '17 at 12:23

0 Answers0