簡體   English   中英

CollectionView與TableView(Swift)

[英]CollectionView vs TableView (Swift)

我有兩個類做的完全相同,在用戶位置附近加載選定類別的位置,但一個使用collectionView,另一個使用tableView(和特定的動畫)。

我只添加了解決該問題的代碼部分,盡管一些名稱不同,但與情節提要和其他類的所有鏈接都是相同的,因為您可以看到這兩個類之間的許多代碼非常相似。

我的問題是具有collectionView的類可以正常工作,但是使用tableView的類不能顯示tableView的單元格(好像沒有正確加載它),如果有人可以看到是否有錯誤,我將不勝感激。

這是使用collectionView的類:

import UIKit
import CoreLocation
import AVFoundation

private let reuseIdentifier = "PlacesCell"

extension UIViewController {
    func present(viewController : UIViewController, completion : (() -> ())? = nil ){
        if let presented = self.presentedViewController {
            presented.removeFromParentViewController()
        }
        self.present(viewController, animated: true, completion: completion)
    }
}

class NearbyPlacesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var locationManager:CLLocationManager?

    let minimumSpacing : CGFloat = 15 //CGFloat(MAXFLOAT)
    let cellWidth: CGFloat = 250
    let radius = 5000 // 5km
    var category : QCategory?
    var currentLocation : CLLocationCoordinate2D?
    var places: [QPlace] = []

    var isLoading = false
    var response : QNearbyPlacesResponse?

    override func viewDidLoad() {
        super.viewDidLoad()

        //self.collectionView
        self.title = category?.name

        collectionView?.contentInset = UIEdgeInsets.init(top: 0, left: minimumSpacing, bottom: 0, right: minimumSpacing)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        determineMyCurrentLocation()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        category?.markView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func canLoadMore() -> Bool {
        if isLoading {
            return false
        }

        if let response = self.response {
            if (!response.canLoadMore()) {
                return false
            }
        }
        return true
    }

    func loadPlaces(_ force:Bool) {

        if !force {
            if !canLoadMore() {
                return
            }
        }

        print("load more")
        isLoading = true
        NearbyPlacesController.getNearbyPlaces(by: category?.name ?? "food", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: didReceiveResponse)
    }

    func didReceiveResponse(response:QNearbyPlacesResponse?, error : Error?) -> Void {
        if let error = error {
            let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
            let actionDismiss = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
            let actionRetry = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
                DispatchQueue.main.async {
                    self.loadPlaces(true)
                }
            })
            alertController.addAction(actionRetry)
            alertController.addAction(actionDismiss)
            DispatchQueue.main.async {
                self.present(viewController: alertController)
            }
        }
        if let response = response {
            self.response = response
            if response.status == "OK" {
                if let placesDownloaded = response.places {
                    places.append(contentsOf: placesDownloaded)
                }

                self.collectionView?.reloadData()
            } else {
                let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
                alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
                alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
                    DispatchQueue.main.async {
                        self.loadPlaces(true)
                    }
                }))
                self.present(viewController: alert)
            }
            isLoading = false
        }
        else {
            print("response is nil")
        }
    }

    // MARK: UICollectionViewDataSource

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

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

        let place = places[indexPath.row]
        cell.update(place: place)

        if indexPath.row == places.count - 1 {
            loadPlaces(false)
        }

        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        performSegue(withIdentifier: "maps-vc", sender: indexPath)
    }

    // MARK: UICollectionViewDelegateFlowLayoutDelegate

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return minimumSpacing
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let cellPadding: CGFloat = 20.0
        let columnWidth:CGFloat = cellWidth
        let imageWidth = columnWidth
        let labelWidth = columnWidth - cellPadding * 2

        let photoHeight = heightForPhotoAtIndexPath(indexPath: indexPath, withWidth: imageWidth)
        let annotationHeight = heightForAnnotationAtIndexPath(indexPath: indexPath, withWidth: labelWidth)
        let height = photoHeight + annotationHeight

        return CGSize.init(width: columnWidth, height: height)
    }

    // Calculates the height of photo
    func heightForPhotoAtIndexPath(indexPath: IndexPath,
                                   withWidth width: CGFloat) -> CGFloat {

        var size = CGSize.init(width: CGFloat(MAXFLOAT), height: 1)
        let place = places[indexPath.row]

        guard let photo = place.photos?.first, place.photos?.first?.photoRef != nil else {
            return 0
        }

        size = CGSize.init(width: CGFloat(photo.width!), height: CGFloat(photo.height!))

        let boundingRect =  CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
        let rect  = AVMakeRect(aspectRatio: size, insideRect: boundingRect)

        return rect.size.height
    }

    // Calculates the height label
    func heightForAnnotationAtIndexPath(indexPath: IndexPath, withWidth width: CGFloat) -> CGFloat {

        let place = places[indexPath.row]
        let annotationPadding = CGFloat(5)

        let font = UIFont.systemFont(ofSize: 15)
        let commentHeight = place.heightForComment(font, width: width)

        let height = annotationPadding + commentHeight + annotationPadding

        return height
    }

    // did receive location

    func didReceiveUserLocation(_ userLocation:CLLocation) {
        currentLocation = userLocation.coordinate

        loadPlaces(true)
    }        
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "maps-vc" && sender is IndexPath {
            let dvc = segue.destination as! MapViewController
            dvc.index = (sender as! IndexPath).row
            dvc.places = places
            dvc.userLocation = currentLocation
        }
     }
}

這是使用tableView的類:

import UIKit
import MapKit
import CoreLocation
import GoogleMaps
import GooglePlaces
import Social
import AVFoundation

private let resueIdentifier = "MyTableViewCell"

extension UIViewController {
    func present(viewController : UIViewController, completion : (() -> ())? = nil ){
        if let presented = self.presentedViewController {
            presented.dismiss(animated: true, completion: {
                self.present(viewController, animated: true, completion: completion)
            })
        } else {
            self.present(viewController, animated: true, completion: completion)
        }
    }
}

class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var locationManager:CLLocationManager?
    let minimumSpacing : CGFloat = 15 //CGFloat(MAXFLOAT)
    let cellWidth: CGFloat = 250
    let radius = 5000 // 5km
    var category : QCategoryy?
    var currentLocation : CLLocationCoordinate2D?
    var places: [QPlace] = []
    var isLoading = false
    var response : QNearbyPlacesResponse?
    var rows = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = category?.name
}

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        determineMyCurrentLocation()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        rows = 0
        insertRowsMode3()
        tableView.reloadData()
        category?.markView()
}
    @IBAction func refreshTapped(_ sender: Any) {
        rows = 0
        insertRowsMode3()
        tableView.reloadData()
    }

    func canLoadMore() -> Bool {
        if isLoading {
            return false
        }
        if let response = self.response {
            if (!response.canLoadMore()) {
                return false
            }
        }
        return true
    }

    func loadPlaces(_ force:Bool) {

        if !force {
            if !canLoadMore() {
                return
            }
        }

        print("load more")
        isLoading = true
        NearbyPlaces.getNearbyPlaces(by: category?.name ?? "food", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: didReceiveResponse)
    }

    func didReceiveResponse(response:QNearbyPlacesResponse?, error : Error?) -> Void {
        if let error = error {
            let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
            let actionDismiss = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
            let actionRetry = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
                DispatchQueue.main.async {
                    self.loadPlaces(true)
                }
            })
            alertController.addAction(actionRetry)
            alertController.addAction(actionDismiss)
            DispatchQueue.main.async {
                self.present(viewController: alertController)

            }
        }
        if let response = response {
            self.response = response
            if response.status == "OK" {
                if let placesDownloaded = response.places {
                    places.append(contentsOf: placesDownloaded)
                }

                self.tableView?.reloadData()
            } else {
                let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
                alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
                alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
                    DispatchQueue.main.async {
                        self.loadPlaces(true)
                    }
                }))
                 self.present(viewController: alert)
            }
            isLoading = false
        }
        else {
            print("response is nil")
        }
    }
    func insertRowsMode2() {

        tableView.beginUpdates()
        for i in 0..<places.count {
            insertRowMode2(ind: i, usr: places[i])
        }
        tableView.endUpdates()
    }

    func insertRowMode2(ind:Int,usr:QPlace) {
        tableView.beginUpdates()
        let indPath = IndexPath(row: ind, section: 0)

        rows = ind + 1
      tableView.insertRows(at: [indPath], with: .right)
       tableView.endUpdates()
    }

    func insertRowsMode3() {
        tableView.beginUpdates()
        rows = 0

        insertRowMode3(ind: 0)
        tableView.endUpdates()
    }
    func insertRowMode3(ind:Int) {
        tableView.beginUpdates()
        let indPath = IndexPath(row: ind, section: 0)
        rows = ind + 1
        tableView.insertRows(at: [indPath], with: .right)

        guard ind < places.count-1 else { return }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.20) {

            self.insertRowMode3(ind: ind+1)
        }
        tableView.endUpdates()
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return  places.count    /*  rows   */
    }
    public  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

        let place = places[indexPath.row]
        cell.update(place: place)

        if indexPath.row == places.count - 1 {
            loadPlaces(false)
        }

        return (cell)
    }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)

        UIView.animate(withDuration: 0.2, animations: {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
    })

        performSegue(withIdentifier: "goToLast" , sender: indexPath.row)
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == UITableViewCellEditingStyle.delete {

            places.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)

        }
    }
    func didReceiveUserLocation(_ userLocation:CLLocation) {
        currentLocation = userLocation.coordinate

        loadPlaces(true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToLast" && sender is IndexPath {

            let dvc = segue.destination as! FinalClass
            dvc.index = (sender as! IndexPath).row
            dvc.places = places
            dvc.userLocation = currentLocation

            }
        }

    @IBAction func IndTapped(_ sender: Any) {
    dismiss(animated: true, completion: nil)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

您是否將CourseClass2類設置為tableView的委托/數據源?

嘗試添加

tableView.dataSource = self
tableView.delegate = self

到您的viewDidLoad方法。

幾乎每次遇到像您這樣的問題時,都是因為我忘記設置委托或數據源。

編輯:我以前只在我的答案上添加了委托,感謝丹尼斯使我想起了dataSource。

我看到您正在執行異步更新,但是同步了reloadData ...

您將必須在asyncAfter內部進行更新,或在您的insertRowMode3中添加一個閉包,並在該閉包中重新加載數據...

你可以試試這個嗎?

func insertRowMode3(ind:Int) {
    DispatchQueue.main.asyncAfter(deadline: .now()+0.20) {
        tableView.reloadData()
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM