【问题标题】:UICollectionView in UITableView - I want to show data in CollectionVewCellUITableView 中的 UICollectionView - 我想在 CollectionVewCell 中显示数据
【发布时间】:2019-04-26 08:32:18
【问题描述】:

我想在UICollectionView 中显示数据。来自UITableView 单元格。我主要关心的是这个。我在另一个 API 中从 cellForRowAt 传递一个密钥 catgID 并从中获取数据。但是数据并没有以正确的方式出现。

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

  1. 这是我的UITableViewtableview 索引。


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 没有进入队列。

【问题讨论】:

    标签: arrays swift uitableview uicollectionview


    【解决方案1】:

    将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
        }
    

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

    【讨论】:

    • 嗨,我已经试过了,但是用这种方式。当我滚动 tableview 时,然后 Id 将进入另一个索引,如:(如果索引 0 类别 id 为 1)滚动后(索引 0 则类别 id 将更改为 2、3、4 分配给索引 0 的另一个类别)
    • @Coding 不会这样改变的。在 cellforrow 中尝试 print(indexPath.row,arrayMenuCat[indexPath.row].id)
    • 打印正常,但屏幕上显示的数据相同
    • @Coding 不要在 viewcontroller 类之外创建任何变量。您的代码令人困惑
    • 请分享您的 WhatsApp 号码,然后我将在 google drive 上分享我的项目。
    猜你喜欢
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 2017-05-29
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多