大佬教程收集整理的这篇文章主要介绍了如何提高 UITableView 的性能,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 UItableVIEw
可以从 firebase 数据库加载图像。表格中的每个单元格包含三张图片。 firestore 查询一次加载三个文档,而表视图在用户滚动到底部时分页。我遇到的问题是,当我滚动表格视图时,每次到达新单元格时都会口吃。每个单元格比全屏多一点。下面是我试图描述的一个例子:https://imgur.com/a/xRB6gZg
以下是产生这些问题的代码:
func paginate(){
postquery = postquery.start(afterdocument: documents.last!)
self.loadposts()
}
//querIEs Firestore and loads into postArray
func loadposts() {
if let blockedArray = userDefaults.array(forKey: blockKey) as? [String]{
blockedUsers = blockedArray
}
postquery.getdocuments{ [weak self](querySnapshot,error) in
self!.q.async{
if let err = error {
print(err)
}else{
var postsTemp = self?.postArray
for doc in querySnapshot!.documents{
self?.documents += [doc]
let post = self!.createPost(doC)
if(!self!.postArray.contains(post) && !self!.blockedUsers.contains(post.uID)){
postsTemp?.append(post)
}
dispatchQueue.main.async {
self!.postArray = postsTemp!
self!.tableVIEw.reloadData()
self!.isNewDataLoading = false
}
}
self!.loadedFirst = true
}
}
}
}
overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: int) -> Int {
if postArray.count == 0{
return 1
}else{
return postArray.count
}
}
overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {
var post: poststruct
var peopleUserIsFollowing: [String] = []
let cell = tableVIEw.dequeueReusableCell(withIDentifIEr: K.cellIDentifIEr,for: indexPath) as! PostCell
cell.delegate = self
if postArray.count == 0 {
let instructions = cell.textLabel
instructions?.text = "Press the camera to start Piking!"
instructions?.textAlignment = .center
clearposts(cell)
}else {
post = postArraY[indexPath.row]
if let leftPostArray = userDefaults.array(forKey: fbleftKey) as? [String]{
Votedleftposts = leftPostArray
}
if let rightPostArray = userDefaults.array(forKey: fbRightKey) as? [String]{
VotedRightposts = rightPostArray
}
let firstReference = storageRef.child(post.firstimageUrl)
let secondReference = storageRef.child(post.secondImageUrl)
//For FrIEndstableVIEw query
let db = Firestore.firestore()
let followingReference = db.collection("following")
.document(currentUser!)
.collection("UserIsFollowing")
followingReference.getdocuments(){(querySnapshot,err) in
if let err = err {
print("Error getTing documents: \(err)")
} else {
for document in querySnapshot!.documents {
peopleUserIsFollowing.append(document.documentID)
}
}
}
//Fill in labels and imageVIEws
cell.timer = createTimer(post,cell)
cell.firstimageVIEw.sd_setimage(with: firstReferencE)
cell.secondImageVIEw.sd_setimage(with: secondReferencE)
cell.lefttitle.text = post.firsttitle
cell.righttitle.text = post.secondtitle
cell.postDescription.text = post.postDescription + "\(indexPath)"
if post.userPic == "" {
userPic =
"https://firebasestorage.GoogleAPIs.com/v0/b/pikit-7e40e4.appspot.com/o/Default%20Profile%20Pic.png?alt=media&token=2bc88382-2ad3-4eb8-8163-dcddf391c666"
} else{
userPic = post.userPic
}
let url = URL(String: userPiC)
let data = try? Data(contentsOf: url!)
cell.profilePic.image = UIImage(data: data!)
let VotesCollection = db.collection("Votes").document(post.postID)
getCount(ref: VotesCollection,cell: cell)
if(post.uID != currentUser){
cell.username.text = post.poster
}else{
cell.username.text = "Me"
cell.tapleft.isEnabled = false
cell.tapRight.isEnabled = false
}
cell.textLabel?.text = ""
if(post.poster == Auth.auth().currentUser!.uID || post.endDate - Int(NSDate().timeIntervalSince1970) <= 0){
cell.tapRight.isEnabled = false
cell.tapleft.isEnabled = false
cell.firstimageVIEw.layer.borderWIDth = 0
cell.secondImageVIEw.layer.borderWIDth = 0
}
else if(VotedRightposts.contains(post.firstimageUrl)){
cell.secondImageVIEw.layer.bordercolor = UIcolor.green.cgcolor
cell.secondImageVIEw.layer.borderWIDth = 4
cell.firstimageVIEw.layer.borderWIDth = 0
cell.tapRight.isEnabled = false
cell.tapleft.isEnabled = true
}
else if (Votedleftposts.contains(post.firstimageUrl)){
cell.firstimageVIEw.layer.bordercolor = UIcolor.green.cgcolor
cell.firstimageVIEw.layer.borderWIDth = 4
cell.secondImageVIEw.layer.borderWIDth = 0
cell.tapleft.isEnabled = false
cell.tapRight.isEnabled = true
}
}
return cell
}
overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,dIDEnddisplaying cell: UItableVIEwCell,forRowAt indexPath: IndexPath) {
let postCell = tableVIEw.dequeueReusableCell(withIDentifIEr: K.cellIDentifIEr,for: indexPath) as! PostCell
clearposts(postCell)
postCell.timer?.invalIDate()
postCell.timer = nil
}
overrIDe func scrollVIEwDIDEndDragging(_ scrollVIEw: UIScrollVIEw,willDecelerate decelerate: Bool) {
//Bottom refresh
if scrollVIEw == tableVIEw{
if ((scrollVIEw.contentOffset.y + scrollVIEw.frame.size.height) >= scrollVIEw.contentSize.height)
{
if !isNewDataLoading{
isNewDataLoading = true
paginate()
}
}
}
}
我尝试调整 dIDEnddisplaying
的作用,例如清除单元格/不清除单元格,但没有效果。我也试过改变 paginate 的调用位置,但这似乎是最好的方法。我不确定我哪里出错了。我还注意到在 Xcode 调试器中,当表格视图上下滚动时,应用程序的内存使用量稳步上升,但似乎从未下降。
通常,您有两种选择可以解决此问题。有很多代码要解析,所以我不能给你一个代码示例,但答案是:
预取
当您滚动到第 2 项时,在向下滚动那么远之前,先开始对第 4、5、6 项的提取(因为您一次提取 3 个)。
此外……您可能会考虑一次获取 3 个以上的内容。比如... 50 或 100。现代 iOS 设备有很多内存。我想不出真正的理由将其限制在如此之少的范围内。
占位符
构建您的布局,使其提供占位符数据,然后异步启动获取以使用真实数据更新屏幕布局。
无论哪种方式都需要您稍微重构一下代码。我的直觉是预取对你来说会更容易。
以上是大佬教程为你收集整理的如何提高 UITableView 的性能全部内容,希望文章能够帮你解决如何提高 UITableView 的性能所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。