Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 第一个单元格和最后一个单元格在UICollectionView中相互更改_Ios_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 第一个单元格和最后一个单元格在UICollectionView中相互更改

Ios 第一个单元格和最后一个单元格在UICollectionView中相互更改,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我有MenuItemCollectionView和FoodItemCollectionView,我正在MenuItemCollectionViewCell中设置FoodItemCollectionView。第一个MenuItemCell和最后一个MenuItemCell中发生的错误正在相互更改。但有时它们出现在正确的位置。我不知道如何修复此错误 这是MenuItemCollectionView func numberOfSections(in collectionView: UICollecti

我有MenuItemCollectionView和FoodItemCollectionView,我正在MenuItemCollectionViewCell中设置FoodItemCollectionView。第一个MenuItemCell和最后一个MenuItemCell中发生的错误正在相互更改。但有时它们出现在正确的位置。我不知道如何修复此错误

这是MenuItemCollectionView

func numberOfSections(in collectionView: UICollectionView) -> Int {
    print("Food Array Count = \(self.foodArray.count)")
    return self.foodArray.count
}

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


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {
    case UICollectionElementKindSectionHeader:
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: RESUABLE_VIEW, for: indexPath) as! CollectionReusableView
        headerView.header.text = nameArray[indexPath.section]
        return headerView
    default:
        assert(false, "Unexpected element kind")
    }

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = UIScreen.main.bounds.width
    let height = UIScreen.main.bounds.height/3
    return CGSize(width: width, height: height)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ITEM_CELL, for: indexPath) as! MenuItemCollectionViewCell
    cell.foodArray.removeAll()
    cell.foodArray = self.foodArray[nameArray[indexPath.section]]!
    print("Cell COol index = \(indexPath.section) , ARrray count = \(cell.foodArray.count)")
    return cell
}
这是MenuItemCollectionViewCell,我在此设置了FoodItemCollectionView

import UIKit
import SDWebImage


class MenuItemCollectionViewCell: UICollectionViewCell ,UICollectionViewDelegate,UICollectionViewDataSource{

    @IBOutlet weak var foodItemColletion: UICollectionView!
    var foodArray :[Food] = []
    var imageArray : [UIImage?] = []
    let FOOD_ITEM_CELL : String = "FoodCell"

    override func awakeFromNib() {
        foodItemColletion.delegate = self
        foodItemColletion.dataSource = self
        super.awakeFromNib()
    }

    override func prepareForReuse() {
        foodArray.removeAll()
        super.prepareForReuse()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        for food in foodArray{
            print("Title Menu item \(food.title)")
        }
        return foodArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FOOD_ITEM_CELL, for: indexPath) as! FoodItemCollectionViewCell
        let foodObj = foodArray[indexPath.row]
        cell.foodLabel.text = foodObj.title
        cell.foodPrice.text = foodObj.price
        cell.foodImage.sd_setImage(with: URL(string: "\(URLS.IMG_URL)\(foodObj.imageUrl)"), placeholderImage: nil, options: .refreshCached, progress: nil, completed: nil)
        print("Title Menu Item Cell \(indexPath.item) = \(foodObj.title)")
        return cell
    }


}
这是FoodItemCollectionViewCell

import UIKit

class FoodItemCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var foodImage: UIImageView!
    @IBOutlet weak var foodLabel: UILabel!
    @IBOutlet weak var foodPrice: UILabel!
    @IBOutlet weak var cardView: CardView!

    override func awakeFromNib() {
        foodImage.layer.masksToBounds = true
        foodImage.roundCorners(corners: [.topLeft,.topRight], radius: 5)
        cardView.setElevation(points: 1.5)
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
        cardView.layer.cornerRadius = 5
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        foodImage.image = nil
    }

}

extension UIImageView{
    func roundCorners(corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}


菜单项集合视图中
更改此方法:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ITEM_CELL, for: indexPath) as! MenuItemCollectionViewCell
    cell.foodArray.removeAll()
    cell.foodArray = self.foodArray[nameArray[indexPath.section]]!

    // add this, or else because of reusing the old data will be presented, even though you have set the new one
    cell.foodItemColletion.reloadData()

    print("Cell COol index = \(indexPath.section) , ARrray count = \(cell.foodArray.count)")
    return cell
}

collectionViewCells.append(cell)
No,不要这样做。在cell.foodArray=self.foodArray[nameArray[indexPath.section]]之后,告诉
单元格.collectionView
重新加载数据()
collectionViewCells.append(单元格)
这是做什么的?对不起,我忘了删除这行…谢谢@Larme,你救了我一天