将数据从集合视图单元格移动到新的集合视图单元格

问题描述 投票:0回答:2

我正在尝试将组织名称传递给新的viewController。问题是,当我将值从集合视图单元格传递到另一个viewController中的新标签时,将显示多个重复相同信息的collectionView单元格。我将如何更改它,以便一旦从第一个collectionView中选择了一个单元格,则只有该信息将填充另一个collectionView单元格?

这是第一个viewController的代码。

import UIKit

class FirstViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var secondCollectionView: UICollectionView!

var valueToPass = Int()

var arrayOfOrganizations = ["one", "two", "three", "four"]

override func viewDidLoad() {
    super.viewDidLoad()

}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let secondCell = secondCollectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
    secondCell.nameTextLabel.text = arrayOfOrganizations[indexPath.item]
    return secondCell
    }

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    valueToPass = indexPath.item
    performSegue(withIdentifier: "MoveCollectionViewCell", sender: self)
    arrayOfOrganizations.remove(at: indexPath.item)
    self.secondCollectionView.deleteItems(at: [indexPath])
    secondCollectionView.reloadData()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "MoveCollectionViewCell") {
        let destination = segue.destination as! MessagesViewController
        destination.valueToPass = arrayOfOrganizations[valueToPass]
    }
}

}


class SecondCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var nameTextLabel: UILabel!
@IBOutlet weak var descriptionTextLabel: UILabel!
}

这是第二个viewController的代码。

import UIKit

class MessagesViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var messagesCollectionView: UICollectionView!

var valueToPass = ""

override func viewDidLoad() {
    super.viewDidLoad()

}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "messagesViewCell", for: indexPath) as! MessagesViewCell
    cell.layer.borderWidth = 3
    cell.layer.cornerRadius = 12
    if (valueToPass == "one") {
        cell.textLabel.text = "one"
    }
    if (valueToPass == "two") {
        cell.textLabel.text = "two"
    }
    if (valueToPass == "three") {
        cell.textLabel.text = "three"
    }
    if (valueToPass == "four") {
        cell.textLabel.text = "four"
    }
    return cell
}

}

class MessagesViewCell: UICollectionViewCell {

@IBOutlet weak var textLabel: UILabel!

}
ios swift uicollectionview uicollectionviewcell
2个回答
1
投票

您的numberOfItemsInSection返回字符串valueToPass的大小(字符数),这是您想要的吗?因为如果只有一个值,则该函数的返回应该为1。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return valueToPass.count // Number of character in valueToPass
}

0
投票

您是numberOfItemsInSection collectionView方法返回的valueToPass.count更改为1。

© www.soinside.com 2019 - 2024. All rights reserved.