使用UIRefreshControl刷新tableView。

huangapple go评论84阅读模式
英文:

Refreshing tableview with UIRefreshControl

问题

我正在使用一个刷新控件,但是虽然我的代码编译通过,但当前的代码存在一个问题,它首先删除所有数据,然后再拉取表格数据,导致出现短暂的没有显示任何数据的情况。有没有快速修复的方法?我是否应该在加载完帖子后调用 removeAll 来使用完成处理程序来解决这个问题...

编辑:我已经添加了下面的 fetchNetworkPost 的相关部分。

override func viewDidLoad() {
    super.viewDidLoad()
    
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
    
    self.refreshControl = refreshControl
    
    requestData()
}

@objc
func requestData() {
    
    self.postArray.removeAll()
    fetchNetworkPosts() //填充数据

    refresh()
}

func refresh() {

    self.refreshControl?.endRefreshing()
        
    self.tableView.reloadData()
}

func fetchNetworkPosts() {
        
    for personId in networkArray {
        
        let currentUserRef = Database.database().reference().child("posts").queryOrdered(byChild: "userId").queryEqual(toValue: personId)
        currentUserRef.observeSingleEvent(of: .value, with: { (snapshot) in
            
            for childSnapshot in snapshot.children {
              
                let test = Post(snapshot: childSnapshot as! DataSnapshot)
                
                self.postArray.append(test)
               
            }
            
            self.postArray.sort { $0.postDate > $1.postDate } //排序

            self.tableView.reloadData()
        })
    }
}

希望这可以帮助你解决问题。

英文:

I'm using a refreshcontrol, but while my code compiles, the current code has an issue where it removes all the data first and then pulls in the table data causing a brief moment where it isn't showing any data. Is there a quick fix for this? Should I use a completion handler for this by calling removeAll once the posts are loaded...

Edit: I've added the relevant portion of fetchNetworkPost below.

override func viewDidLoad() {
super.viewDidLoad()
    
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
    
    self.refreshControl = refreshControl
    
    requestData()
}

@objc
func requestData() {
    
    self.postArray.removeAll()
    fetchNetworkPosts() //populates data

    refresh()

}

func refresh() {

  self.refreshControl?.endRefreshing()
        
  self.tableView.reloadData()
    
}

func fetchNetworkPosts() {
        
        for personId in networkArray {
            
    let currentUserRef = 
Database.database().reference().child("posts").queryOrdered(byChild: 
"userId").queryEqual(toValue: personId)
            currentUserRef.observeSingleEvent(of: .value, with: { 
(snapshot) in
                
                for childSnapshot in snapshot.children {
                  
                    let test = Post(snapshot: childSnapshot as! DataSnapshot)
                    
                    self.postArray.append(test)
                   
                }
                
                self.postArray.sort { $0.postDate > $1.postDate } //sort
        
                self.tableView.reloadData()
                
            })
            
        }
}

答案1

得分: 1

fetchNetworkPosts()方法中,当你接收到新数据时,执行以下操作:

self.postArray.removeAll() // 移除旧数据
for childSnapshot in snapshot.children {
    let test = Post(snapshot: childSnapshot as! DataSnapshot)
    self.postArray.append(test)
}
self.postArray.sort { $0.postDate > $1.postDate } // 排序
self.refresh() // 调用refresh()

希望这对你有帮助。

英文:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
    
    self.refreshControl = refreshControl
    
    requestData()
}
@objc func requestData() {
    fetchNetworkPosts() //populates data
}

func refresh() {
    
    self.refreshControl?.endRefreshing()
    
    self.tableView.reloadData()
    
}

func fetchNetworkPosts() {
    
    for personId in networkArray {
        
        let currentUserRef =
            Database.database().reference().child("posts").queryOrdered(byChild:
                "userId").queryEqual(toValue: personId)
        currentUserRef.observeSingleEvent(of: .value, with: {
            (snapshot) in
            
            self.postArray.removeAll()
            for childSnapshot in snapshot.children {
                
                let test = Post(snapshot: childSnapshot as! DataSnapshot)
                
                self.postArray.append(test)
                
            }
            
            self.postArray.sort { $0.postDate > $1.postDate } //sort
            
            self.refresh()
            
        })
        
    }
}

inside fetchNetworkPosts() method when you receive the new data
self.postArray.removeAll() remove old data there and then assign new value at that time. then call refresh().
Hope this will work for you.

答案2

得分: 1

在fetchNetworkPosts()完成后,请调用refresh()方法,如下所示。请查看以下代码。

@objc func requestData() {
    fetchNetworkPosts() // 填充数据
}

func refresh() {

    self.refreshControl?.endRefreshing()
    self.tableView.reloadData()

}

func fetchNetworkPosts() {

    self.postArray.removeAll() // 在这里移除数组数据

    for personId in networkArray {

        let currentUserRef =
            Database.database().reference().child("posts").queryOrdered(byChild:
            "userId").queryEqual(toValue: personId)
        currentUserRef.observeSingleEvent(of: .value, with: {
            (snapshot) in

            for childSnapshot in snapshot.children {

                let test = Post(snapshot: childSnapshot as! DataSnapshot)

                self.postArray.append(test)

            }

            self.postArray.sort { $0.postDate > $1.postDate } // 排序

            self.refresh()

        })

    }
}
英文:

Call your refresh() after the completion of fetchNetworkPosts() like below. Check the code below.

@objc func requestData() {
    fetchNetworkPosts() //populates data
}

func refresh() {

  self.refreshControl?.endRefreshing()
  self.tableView.reloadData()

}

func fetchNetworkPosts() {

         self.postArray.removeAll() // remove your array data here

        for personId in networkArray {

        let currentUserRef =
Database.database().reference().child("posts").queryOrdered(byChild:
"userId").queryEqual(toValue: personId)
            currentUserRef.observeSingleEvent(of: .value, with: {
(snapshot) in

                for childSnapshot in snapshot.children {

                    let test = Post(snapshot: childSnapshot as! DataSnapshot)

                    self.postArray.append(test)

                }

                self.postArray.sort { $0.postDate > $1.postDate } //sort

                self.refresh()

            })

        }
}

答案3

得分: 0

也许改变顺序可以解决你的问题。

override func viewDidLoad() {
    super.viewDidLoad()
    
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
    
    self.refreshControl = refreshControl
    
    requestData()
}

@objc
func requestData() {
    
    fetchNetworkPosts() //populates data
    
    refresh()
}

func refresh() {
    self.postArray.removeAll()
    self.refreshControl?.endRefreshing()
    
    self.tableView.reloadData()
}

你在获取数据之前移除了数据。所以,先获取数据,然后再清空你的 tableView。你会在短暂的时间内看到旧数据,而不是空的 tableView。

英文:

maybe changing order will fix your problem.

override func viewDidLoad() {
super.viewDidLoad()

    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)

    self.refreshControl = refreshControl

    requestData()
}

@objc
func requestData() {

    
    fetchNetworkPosts() //populates data

    refresh()

}

func refresh() {
  self.postArray.removeAll()
  self.refreshControl?.endRefreshing()

  self.tableView.reloadData()

}

You are removing your data before fetch it. So, fetch data first then remove your tableView. You'll see your old data for brief time, not empty tableView.

huangapple
  • 本文由 发表于 2020年1月3日 13:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定