gpt4 book ai didi

ios - 加载消息时滚动到 UITableView 的底部

转载 作者:搜寻专家 更新时间:2023-11-01 05:58:49 24 4
gpt4 key购买 nike

我正在尝试弄清楚一旦来自数据库的消息加载到其中,如何滚动到 UITableView 的底部。我试过使用 tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true) 但我的表从包含所有消息到什么都没有。我也试过了

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

但我得到一个 EXC_BAD_ACCESS code = 1。任何帮助将不胜感激。

编辑:

在 Parse 从 query.findObjectsInBackgroundWithBlock(...) 中找到数据后,我立即将此代码放入我的 viewDidAppear() 函数中,不确定这是否会产生影响。

查询代码如下:

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

最佳答案

试试这个

- (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];
}
}

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

关于ios - 加载消息时滚动到 UITableView 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30253424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com