2

I'm getting an EXC_BAD_ACCESS error when using scrollToRowAtIndexPath in the viewWillAppear method. I searched for solutions and saw some old posts recommending to set delegate and table to nil (see code below), however when I set that I simply dont get anything loaded in my tableview. I should say that this is part of a chat application where I want to show the last message entered first. Many thanks for any assistance with this. Here's my viewWillAppear:

-(void)viewWillAppear:(BOOL)animated {
    [self.table reloadData];
    int lastRowNumber = [self.table numberOfRowsInSection:0] - 1;
    NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
    //self.table.delegate = nil;
    //self.table = nil;
    [self.table scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

I should add that this code seems to work fine in other parts of my program, the only time I get the error is in the viewWillAppear method.

Kitcc
  • 2,138
  • 4
  • 24
  • 42

4 Answers4

2

viewWillAppear: is too early to do any animation on view. Per Apple Documentation:

This method is called before the view controller's view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

So, you cannot add animations when even the view hierarchy is not set completely.

Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

It may help you

NSIndexPath * lastIndex =[NSIndexPath indexPathForRow:yourContenetArray.count-1 inSection:0];

[self.yourTableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionNone animated:YES];
Anshad Rasheed
  • 2,526
  • 1
  • 14
  • 32
  • Hi, thanks but I still get the same EXC_BAD_ACCESS error. Actually this code does work in other parts of my program, the error only occurs in viewWillAppear. – Kitcc Oct 21 '15 at 05:02
0
Write your code in viewDidAppear method.As your table view is loaded 
then after you should call the method scrollToRowAtIndexPath . Then 
your animation will be performed.And if you want to use in 
viewWillAppear , then You can try to reload your tableView and then
write code for scrollToRowAtIndexPath
Vatsal Raval
  • 311
  • 1
  • 9
  • Hi thanks for the response, however this does not clear the error. I get the same error whether I am using the ViewWillAppear or ViewDidAppear. – Kitcc Oct 28 '15 at 05:32
  • hi , have you tried your code after reloading the tableview. ? – Vatsal Raval Oct 28 '15 at 06:00
  • Yes I put a reload into the ViewDidAppear before the code that causes the error, but still get the error. Or are you suggesting to put it elsewhere? – Kitcc Oct 28 '15 at 06:43
  • Use reload tableview after that write your code for scrolToRowAtIndexPath in viewWillAppear only , or Else write the same thing in viewDidAppear. – Vatsal Raval Oct 28 '15 at 07:46
0

You should probably use the viewDidLoad method for scrolling, and if your array count is 0 (empty array) it will throw an error. It is not possible to scroll to cell at index -1.

Minebomber
  • 1,209
  • 2
  • 12
  • 35