简体   繁体   中英

Scrolling to Bottom of UITableView When Messages Load

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)
                    }
    }

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)
    }

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)

        }

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.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM