如何在swift中使用uirefreshcontroll在tableView单元格内重新加载JSON CollectionView单元格

时间:2019-05-14 08:12:11

标签: json swift alamofire swifty-json uirefreshcontrol

我使用viewcontroller创建一个应用程序,在viewcontroller内部有表视图和集合视图。我尝试在ViewController中使用uirefreshcontrol在表视图单元中重新加载json,但未加载json。

ViewController> TableView> TableViewCell> CollectionView> CollectionViewCell。

这是我的viewController

import UIKit

class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!
var refreshControl = UIRefreshControl()
var reloadData : MainTableViewCell!

override func viewDidLoad() {
    super.viewDidLoad()
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(handleRefresh(_:)), for: UIControl.Event.valueChanged)
    tableView.addSubview(refreshControl)
}

@objc func handleRefresh(_ refreshControl: AnyObject) {
    self.tableView.reloadData()
    self.reloadData?.fetchData()

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
        refreshControl.endRefreshing()
    }

}

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

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

这是我的tableview单元格

import UIKit
import SwiftyJSON
import Alamofire 
import Kingfisher

class MainTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

var mainModel : [ModelMain] = []
@IBOutlet weak var collView: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    fetchData()

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return mainModel.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCollectionViewCell", for: indexPath) as! MainCollectionViewCell

    let url = mainModel[indexPath.row].images
    cell.titleImg.kf.setImage(with: URL(string: url), placeholder: UIImage(named: "cancel"), options: [.transition(.fade(1)), .scaleFactor(UIScreen.main.scale)])

    return cell
}

func fetchData(){
    Alamofire.request("https://jsonplaceholder.typicode.com/photos", method: .get, encoding: URLEncoding.default).responseJSON{

        (response) in

        switch response.result{
        case .success(let value):
            let json = JSON(value)

            for item in json.array!{
                let thumbnailUrl = item["thumbnailUrl"].string

                self.mainModel.append(ModelMain(images: thumbnailUrl ?? ""))
                print(self.mainModel.count)
            }
            self.collView.reloadData()
        case .failure(let error):
            print(error)
        }
    }
}
}

我尝试对func fetchData()进行注释,以检查是否重新加载数据,但是不重新加载数据。

0 个答案:

没有答案
相关问题