1

I'm trying to figure out how to scroll to the bottom of the UITableView once the messages from the database load into it. I've tried using tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true) but my table goes from having all the messages in it to nothing at all. I've also tried

var iPath = NSIndexPath(forRow: messagesArray.count - 1, inSection: 0)
tableView.scrollToRowAtIndexPath(iPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

but I get a EXC_BAD_ACCESS code = 1. Any help would be appreciated.

EDIT:

I'm putting this code in my viewDidAppear() function right after Parse finds the data from the query.findObjectsInBackgroundWithBlock(...), not sure if that makes a difference or not.

Here's the query code:

    query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
                    if (error == nil) {
                        for var i = 0; i < object!.count; i++ {
                            var messageObject = Messages()

                            messageObject.messageID = object![i]["messageID"] as! String

                            println("MESSAGE ID")
                            println(messageObject.messageID)

                            messageObject.messageText = object![i]["message"] as! NSString as String

                            println("MESSAGE TEXT")
                            println(messageObject.messageText)

                            messageObject.recipientID.append(object![i]["recipientID"] as! NSString as String)

                            println("RECIPIENT")
                            println(messageObject.recipientID[0])

                            messageObject.senderID = object![i]["senderID"] as! NSString as String
                            messageObject.timeStamp = object![i]["timeStamp"] as! NSString as String

                            println("TIMESTAMP")
                            println(messageObject.timeStamp)

                            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                                self.messagesArray.append(messageObject)
                            })
                        }


                        self.loadObjects()
                        self.tableView.reloadData()
                        self.tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true)
                    }
    }
Navio53
  • 591
  • 1
  • 6
  • 20

4 Answers4

2

Try This

- (void) viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  if (tableView.contentSize.height > tableView.frame.size.height) 
    {
      CGPoint offset = CGPointMake(0, tableView.contentSize.height -tableView.frame.size.height);
      [tableView setContentOffset:offset animated:YES];
   }
}

IN SWIFT

if tableView.contentSize.height > tableView.frame.size.height
    {
        let offset = CGPoint(x: 0, y: tableView.contentSize.height - tableView.frame.size.height)
        tableView.setContentOffset(offset, animated: true)
    }
Shruti
  • 1,849
  • 1
  • 13
  • 21
2

Please try this one

In Objective-c

 NSInteger bottomRow = [self.dataArr count] - 1; // this is your count's array.
    if (bottomRow >= 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:bottomRow inSection:0];
        [self.tblview scrollToRowAtIndexPath:indexPath
                               atScrollPosition:UITableViewScrollPositionTop animated:animated];
    }

in swift

var bottomRow : NSInteger = dataArr.count-1;
if (bottomRow >= 0) {

            var indexPath : NSIndexPath = NSIndexPath(forRow: bottomRow, inSection: 0)


            tblview.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

        }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
2

I called below method and it worked for me. But, make sure to call it before reloading your tableview.

[tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; 
[tableView reloadData];

Hope it will help you.

0

When you table get loaded then scroll it after few time.

- (void)viewDidApear{
 //Please convert it into swift.
   [self performSelector:@selector(scrollToBottom:) withObject:nil afterDelay:0.5];
}

- (void)scrollToBottom:(id)sender{

// Scroll to bottom here

    var iPath = NSIndexPath(forRow: messagesArray.count - 1, inSection: 0)
tableView.scrollToRowAtIndexPath(iPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}
abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31