UITableView中的UICollectionView-我想在CollectionVewCell中显示数据

时间:2019-04-26 08:32:18

标签: arrays swift uitableview uicollectionview

我想在UICollectionView中显示数据。哪个来自UITableView单元格。我主要关心的是这个。我正在通过另一个API中的catgID传递密钥cellForRowAt并从中获取数据。但是数据无法正确传输。

我正在从catgID传递cellForRowAt并进入另一个API,该API将显示UICollectionViewCells的数据列表。现在数据来了,但是方式不正确。

  1. 这是我的UITableView索引的tableview类。


import UIKit
import Reachability
import Alamofire


var arrayMenuProducts = [structMenuProducts]()
struct structMenuProducts {
    var id:Int
    var product_name:String
    var category:String
    var product_image:String
    var price:String
    var unit_price:Double
    var addons:NSArray
}


class MenuVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var reachability = Reachability()!

    @IBOutlet weak var tableview: UITableView!


    var arrayMenuCat = [structMenuCat]()
    struct structMenuCat{
        var id:Int
        var category_name:String
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        menuVegAPI()
    }


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


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        catgID = arrayMenuCat[indexPath.row].id

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1") as! MenuTableCell
        cell.lblCategoryTitle.text = arrayMenuCat[indexPath.row].category_name
        cell.collectionviewOne.reloadData()
//        let catid = arrayMenuCat[indexPath.row].id
//        print(catid)


        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 216
    }




    func menuVegAPI()
    {
        if (reachability.connection == .wifi) || (reachability.connection == .cellular)
        {
            arrayMenuCat.removeAll()
            SwiftLoader.show(animated: true)
            let url = BaseUrl + ViewController.sharedInstance.menuCategory
            print(url)

            Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default).responseJSON { response in
                SwiftLoader.hide()
                switch response.result {
                case .success:
                    let json = response.result.value
                        print(json)

                    let code = (json as AnyObject).object(forKey: "code") as! Int
                    print(code)

                    if code == 200
                    {
                        let data = (json as AnyObject).object(forKey: "data") as? NSArray

                        for alldata in data!
                        {
                            let id = (alldata as AnyObject).object(forKey: "id") as! Int
                            let category_name = (alldata as AnyObject).object(forKey: "category_name") as! String
                            let arr = structMenuCat(id: id, category_name: category_name)
                            self.arrayMenuCat.append(arr)
//                            self.menuProductsAPI(categoryid: id)

                        }
                        self.tableview.reloadData()

                    }
                    else
                    {

                    }
                case .failure:
                    print("error")
                }
            }
        }
        else
        {
            alert(title: "", message: "Please Check Your Internet Connection")
        }

    }
}

  1. 这是我的TableViewCell类型的类。在本课程中,我向CollectionView显示数据。它的代码在这里


import UIKit
import Alamofire
import Reachability

var catgID : Int!
var collectionreload : UICollectionView?

class MenuTableCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {


    var reachability = Reachability()!

    @IBOutlet weak var lblCategoryTitle: UILabel!
    @IBOutlet weak var collectionviewOne: UICollectionView!


    var arrayMenuProducts = [structMenuProducts]()
    struct structMenuProducts {
        var id:Int
        var product_name:String
        var category:String
        var product_image:String
        var price:String
        var unit_price:Double
        var addons:NSArray
    }


    override func awakeFromNib() {
        super.awakeFromNib()
        collectionreload = self.collectionviewOne
        print(arrayMenuProducts)
        print(catgID ?? 0)

        menuProductsAPI(categoryid: catgID!)
    }




    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 arrayMenuProducts.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellRID", for: indexPath) as! MenuCollectionViewCell

        let abc = arrayMenuProducts[indexPath.row].product_name
            print(abc)

            //        if catgID == Int(arrayMenuProducts[indexPath.row].category)
            //        {
            cell.lblTitleForVeg.text = arrayMenuProducts[indexPath.row].product_name
            cell.lblForPriceVeg.text = "$\(arrayMenuProducts[indexPath.row].unit_price)"

    }


    func menuProductsAPI(categoryid:Int)
    {
        if (reachability.connection == .wifi) || (reachability.connection == .cellular)
        {
            SwiftLoader.show(animated: true)
            arrayMenuProducts.removeAll()
            let url = BaseUrl + ViewController.sharedInstance.menuProducts + "categoryid=\(categoryid)"
            print(url)

            Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default).responseJSON { response in

                switch response.result {
                case .success:
                    let json = response.result.value
                    print(json)
                    //                    self.tableview.reloadData()
                    let code = (json as AnyObject).object(forKey: "code") as! Int
                    print(code)

                    if code == 200
                    {
                        let data = (json as AnyObject).object(forKey: "data") as? NSArray

                        DispatchQueue.main.async {
                            for alldata in data!
                            {
                                let id = (alldata as AnyObject).object(forKey: "id") as! Int
                                let product_name = (alldata as AnyObject).object(forKey: "product_name") as! String
                                let category = (alldata as AnyObject).object(forKey: "category") as! String
                                let product_image = (alldata as AnyObject).object(forKey: "product_image") as! String
                                let price = (alldata as AnyObject).object(forKey: "price") as! String
                                let unit_price = (alldata as AnyObject).object(forKey: "unit_price") as! Double
                                let addons = (alldata as AnyObject).object(forKey: "addons") as? NSArray


                                let arr = structMenuProducts(id: id, product_name: product_name, category: category, product_image: product_image, price: price, unit_price: unit_price, addons: addons!)
                                self.arrayMenuProducts.append(arr)
                            }
                            self.collectionviewOne.reloadData()
                            SwiftLoader.hide()
                        }
                    }
                    else
                    {

                    }
                case .failure:
                    print("error")
                }
            }
        }
        else
        {
            //            alert(title: "", message: "Please Check Your Internet Connection")
        }

    }

}

我想以适当的格式显示来自CollectionView的数据。如果Tableview index == 0并且类别ID为10,则为。类别ID 10将首先按照我要传递类别ID的顺序依次排列。就我而言,类别ID不在队列中。

1 个答案:

答案 0 :(得分:0)

将tableview数据源方法更新为

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1") as! MenuTableCell
        cell.lblCategoryTitle.text = arrayMenuCat[indexPath.row].category_name    
        cell.menuProductsAPI(categoryid: arrayMenuCat[indexPath.row].id)
        return cell
    }

然后从menuProductsAPI(categoryid: catgID!)方法中删除awakeFromNib