我正在研究一个聊天应用程序示例。
我想在加载所有数据后向下滚动,但是我的代码不起作用。它会加载所有数据,对它们进行排序并显示,但不会向下滚动到最后一条消息。
这是我的代码,我在其中加载所有数据并想向下滚动:
//Downloads messages
func fetchData() {
Message.downloadAllMessages(forUserID: (currentUser?.uid)!, completion: {[weak weakSelf = self] (message) in
weakSelf?.items.append(message)
weakSelf?.items.sort{ $0.timestamp < $1.timestamp }
DispatchQueue.main.async {
if let state = weakSelf?.items.isEmpty, state == false {
weakSelf?.tableView.reloadData()
weakSelf?.tableView.scrollToRow(at: IndexPath.init(row: self.items.count - 1, section: 0), at: .bottom, animated: false)
}
}
})
Message.markMessagesRead(forUserID: (currentUser?.uid)!)
}
这是我的自定义:
func customization() {
self.imagePicker.delegate = self
self.tableView.estimatedRowHeight = self.barHeight
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.contentInset.bottom = self.barHeight
self.tableView.scrollIndicatorInsets.bottom = self.barHeight
self.navigationItem.title = self.currentUser?.username
self.locationManager.delegate = self
//ContainerView customization
let extraViewsContainer = UIView.init()
extraViewsContainer.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(extraViewsContainer)
self.topAnchorContraint = NSLayoutConstraint.init(item: extraViewsContainer, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 1000)
self.topAnchorContraint.isActive = true
extraViewsContainer.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
extraViewsContainer.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
extraViewsContainer.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1).isActive = true
extraViewsContainer.backgroundColor = UIColor.clear
//PreviewView Customization
extraViewsContainer.addSubview(self.previewView)
self.previewView.isHidden = true
self.previewView.translatesAutoresizingMaskIntoConstraints = false
self.previewView.leadingAnchor.constraint(equalTo: extraViewsContainer.leadingAnchor).isActive = true
self.previewView.topAnchor.constraint(equalTo: extraViewsContainer.topAnchor).isActive = true
self.previewView.trailingAnchor.constraint(equalTo: extraViewsContainer.trailingAnchor).isActive = true
self.previewView.bottomAnchor.constraint(equalTo: extraViewsContainer.bottomAnchor).isActive = true
self.scrollView.minimumZoomScale = 1.0
self.scrollView.maximumZoomScale = 3.0
//MapPreView Customization
extraViewsContainer.addSubview(self.mapPreviewView)
self.mapPreviewView.isHidden = true
self.mapPreviewView.translatesAutoresizingMaskIntoConstraints = false
self.mapPreviewView.leadingAnchor.constraint(equalTo: extraViewsContainer.leadingAnchor).isActive = true
self.mapPreviewView.topAnchor.constraint(equalTo: extraViewsContainer.topAnchor).isActive = true
self.mapPreviewView.trailingAnchor.constraint(equalTo: extraViewsContainer.trailingAnchor).isActive = true
self.mapPreviewView.bottomAnchor.constraint(equalTo: extraViewsContainer.bottomAnchor).isActive = true
//NotificationCenter for showing extra views
NotificationCenter.default.addObserver(self, selector: #selector(self.showExtraViews(notification:)), name: NSNotification.Name(rawValue: "showExtraView"), object: nil)
}
我在viewDidLoad中同时调用fetchData
和customization
,也许customization
中有一些问题
在此先感谢您的帮助!
答案 0 :(得分:1)
您可以尝试在selectRow
之前使用didSelectRowAt
和scrollToRow
,
DispatchQueue.main.async {
if let state = weakSelf?.items.isEmpty, state == false {
weakSelf?.tableView.reloadData()
let indexPath = IndexPath.init(row: self.items.count - 1, section: 0)
weakSelf?.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
weakSelf?.tableView.delegate?.tableView?(weakSelf?.tableView, didSelectRowAt: indexPath)
weakSelf?.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}