ios - UIRefreshControl 未按正确顺序加载

标签 ios swift

当我慢慢下拉刷新时,它得到的新内容的顺序不正确。

此外,当我拉刷新时它所堆叠的触发器不会给出任何错误,它只是在 3 个单元格下加载新内容。

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableview: UITableView!

var url: String = "http://www.test.com/?json=get_recent_posts"

//var url: String = "http://www.test.com/wp-json/wp/v2/posts"

var refreshControl: UIRefreshControl = UIRefreshControl()

var posts: [Post]? = []


// Navigation 
override func viewDidAppear(_ animated: Bool) {
    let nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.black
    self.navigationController?.navigationBar.barTintColor = UIColor.white

    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    imageView.contentMode = .scaleAspectFit
    let image = UIImage(named: "logo")
    imageView.image = image
    navigationItem.titleView = imageView

}



override func viewDidLoad() {
    super.viewDidLoad()

    func stringFromHTML( string: String?) -> NSAttributedString?
    {


        do {
            let data = string?.data(using: String.Encoding.utf8, allowLossyConversion: true)
            if let d = data {
                let str = try NSAttributedString(data: d,
                                                 options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                 documentAttributes: nil)
                return str
            }
        } catch {
        }
        return nil
    }



    fetchPosts()
    // Refresh

    refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Refresh")
    refreshControl.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged)
    tableview.addSubview(refreshControl) // not required when using UITableViewController
    tableview.sendSubview(toBack: refreshControl)
    refresh()

}

func refresh() {
    //print("Refresh table")
    fetchPosts()
}

func fetchPosts() {
    if self.url == "" {
        // print("no posts")
        DispatchQueue.main.async {

            if (self.refreshControl.isRefreshing) {
                self.refreshControl.endRefreshing()
            }

            self.tableview.reloadData()

            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }
        return
    }

    let urlRequest = URLRequest(url: URL(string: self.url)!)

    let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in

        if error != nil {
            print(error!)
            return
        }

        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]

            if let postsFromJson = json["posts"] as? [[String:AnyObject]]
            {
                for post_json in postsFromJson {
                    let post = Post()
                    if let title = post_json["title"] as? String{
                        post.title = title

                    }
                    if let attachments = post_json["attachments"] as? [[String: AnyObject]] {
                        if let attachment = attachments[0] as? [String: AnyObject] {
                            if let imageUrl = attachment["url"] as? String {
                                post.imageUrl = imageUrl
                            }
                        }
                    }
                    if let content = post_json["content"] as? String {
                        post.content = content
                    }


                self.posts?.append(post)
            }

                DispatchQueue.main.async {
                    self.tableview.reloadData()
                }
        }
        } catch let error {
            print(error)
        }
    }

    task.resume()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postsCell", for: indexPath) as! PostsCell

    cell.title.text = self.posts?[indexPath.row].title
    cell.imgView.downloadImage(from:(self.posts?[indexPath.row].imageUrl)!)

    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.posts?.count ?? 0
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "showDetail") {
            let DVC = segue.destination as! DetailViewController
            if let indexpath = self.tableview.indexPathForSelectedRow {
                let title = self.posts?[indexpath.row].title
                DVC.title_text = title
                DVC.img_url = self.posts?[indexpath.row].imageUrl
                DVC.content_text = self.posts?[indexpath.row].content
            }
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.tableview.reloadData()
    }

}


extension UIImageView {

    func downloadImage(from url: String){
        let encoded_url = url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
        let urlRequest = URLRequest(url: URL(string: encoded_url!)!)

        let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
            if error != nil {
                print(error!)
                return
            }

            DispatchQueue.main.sync {
                self.image = UIImage(data: data!)
            }
        }
        task.resume()
    }
}

最佳答案

UIRefreshControl 与数据的显示顺序无关, TableView 将渲染 0-n 的部分和行——无论数据来自哪里(数组、集合等)都需要应用排序逻辑到它。

关于ios - UIRefreshControl 未按正确顺序加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41530819/

相关文章:

swift - 如何在调用 `viewDidLoad` 之前加载 self.navigationController

ios - 如何解析核心数据的数据来构造

ios - 在@ViewBuilder 闭包中更改@State

ios - void(^)(NSData*) 是什么意思?

ios - 带有overlayskscene Xcode Swift 的屏幕触摸Scenekit 导致应用程序崩溃

swift - 为什么必须初始化一个声明为可选的常量?

ios - 不同语言的 URL,如何在 Swift 中转换 URL?

ios - 将核心数据文件模型从 Objective-C 应用程序复制到全新的 Swift 3 项目

ios - Swift 语法问题 : Nested class type cannot be used when declaring variables of that type

iphone - 查找 NSString 内由字符串分隔的范围